iOS: How to know reloadData() was completed its task?

Harshil Kotecha picture Harshil Kotecha · Apr 26, 2017 · Viewed 13.4k times · Source

I want to scroll to a given index (self.boldRowPath), but when I debug scrollToRow is performed before reloadData().

How to know reloadData has finished ?

func getAllTimeEvent()  {
    self.arrAllTimeEvent = ModelManager.getInstance().getAllTimeEvent(from: self.apportmentDateFrom, to: self.apportmentDateTo)
    self.tblTimeEvent.reloadData()
    self.tblTimeEvent.scrollToRow(at: self.boldRowPath ?? [0,0], at: .top, animated: true)
 }

Any help will be much appreciated.

Answer

Farid Al Haddad picture Farid Al Haddad · Apr 26, 2017

Try doing this, it should work:

self.tblTimeEvent.reloadData()
DispatchQueue.main.async(execute: {
    self.tblTimeEvent.scrollToRow(at: self.boldRowPath ?? [0,0], at: .top, animated: true)
})

This will execute the scrollToRow on the main thread, that means after the reloadData is done (because it is on the main thread)