Reputation: 600
Issue:
I have a UITableView
that is reloaded by a controller as soon as it receives an NSNotification
. The data structure that the cellForRowAtIndexPath
uses as the datasource
may change while the table is refreshing.
Background:
Whenever the app's data model changes a NSNotification
gets fired and my UITableViewController
who handles the datasource
of the UITableView
gets notified to execute a "refreshReload
" method. The "refreshReload
" method retrieves the new data from the Model and then asks for [tableView reloadData]
. This is classes MVC pattern where the model gets changed, the controller gets notified and the view gets updated. App crashes when there are quick Notifications back to back that change the data quickly. I feel that while the table is calling cellForRowAtIndexPath
: the data structure that contains the data changes during the execution.
What would be a good pattern to follow to avoid this, is there a way to stop reloading of a table so that I can first perform the stop then change the datasource
?
Upvotes: 1
Views: 1296
Reputation: 830
The crash will be come if the data change very frequently i.e. at the time table is updating and table get another reload call.
To resolve crashes in your app:
First thing you can do is that avoid that much frequent calling of reload table. Maintain a Flag (in Notification observer method that you set for receiving notification) ,that flag tells that table need to reload now when user come to this tableview screen you create a timer that will call a method after (say) every one minute . In that method, check for flag , if it is YES then reload the table and Change Flag value to NO else there is nothing you need to do.
I hope that will resolve your problem for sure.
Upvotes: 0
Reputation: 4371
If I understand your question correctly, I would use global flag to lock data changes.
e.g. use singleton
pattern to hold your flag value. Check whether flag is locked or not. If flag is locked, do not call or disable data change methods. When UITableView is updating, lock flag until updating is finished.
Upvotes: 1