Am currently working on a app that fetches data (about 8000 rows) from a Web Service and saves them in a Realm database in a background thread.
But after saving those data, I send a notification via delegation and I reload the UITableView which blocks the UI for a few seconds (~ 5 seconds)
I was wondering if there is a way to reload smoothly the tableView without latency.
Thank you in advance for your responses and Happy New Year
If the data is all local then this should not happen… the table should only load those cells that are currently visible. If you have so many visible that they cannot be loaded without making the UI unresponsive then maybe your design needs a rethink. Sounds like you are doing the right thing by fetching in a background thread.
But do some investigation - use Instruments to time profile your app and find out exactly what is going wrong. One time I had an unresponsive table view and it was only when I tried Instruments that I found that creating a NSDateFormatter is a very expensive operation and was causing all my problems.
Ideally your data fetch should be happening in an asynchronous network operation which will not have any impact on UI responsiveness. Only when all the data is available should you use reloadData(). If your table requires supplemental data such as an image when it is loaded, use a default image until it is ready and then make another asynchronous fetch, changing the image to the final image only when it is received.
And of course if you receive notification that the background operation has finished asynchronously, that’s on the background thread and you need to be sure to call reloadData() on the main thread since it is a UI operation.
Hey thank you @aeberbach for the detailed response.
Yes, I subclass Operation and create an asynchronous operation that fetches the data from the Web Service. It’s just text basically, no images whatsoever. But when the operation is finished and the array of data replaced and I call reloadData() then the UI hangs for 6 seconds.
Am going to follow your advice and use Instruments in order to find out what causes the issue.
My guess is that there is a substantial amount of processing going on with all that received text, and whatever it is must be on the main thread - possibly the handling of the received data needs to be moved to the background thread so that it is available, fully ready, for the table view. But Instruments is the definitive way to find out. Interested to hear what you discover!
After using Instruments to profile the App, it turns out the data processing is taking few seconds and is done in the Main Thread, that’s why it hangs a couple of seconds.
But the problem is my Operations are asynchronous, how come the JSON processing is done in the main thread?
Well done on finding the problem! Without seeing the code I don’t know why it is on the main thread for sure but I am guessing that it is because some UI processing is expected at that point. Can you just dispatch that JSON processing in a background thread? You will need to end that background thread by dispatching reloadData on the main thread.
Thank you for your suggestion. This is exactly what I’ve done. Dispatch the JSON data processing on a background thread and when it’s done I return it in the main thread.
Thank you very much for your help.