I am iterating through an array of text, with each iteration I am:
- creating a managed object using initWithEntity:insertIntoManagedObjectContext
- parsing text
- determining the display order
With the newly created objects ending up in a tableview.
Because of the super-cool free functionality of the fetchResultsController, cells get automatically inserted into the tableview as insertIntoManagedObjectContext is called.
The problem is there a slight delay in processing the each new object (~20ms) and the order of the lines of text are differently ordered than the tableview that is being updated. This makes the screen flicker as all the cells figure out their display order, with other cells being inserted rapidly behind them. (I can post a video of this effect if this is unclear, I didn’t think it would be this hard to describe it.)
It would be logical to take the lines of text and first parse them into another array, and then insert THAT array into the moc, but I can’t because these are managed objects, and I can’t just alloc them – I have to call initWithEntity:insertIntoManagedObjectContext, which in turn automatically inserts the cells into the tableview.
Taking out the insertRowsAtIndexPaths:withRowAnimation in the didChangeObject method and just calling a reloadData when the iteration was finished didn’t work. (Should it?)
I just want the table not to update until after all the objects have been created.
I can’t see a way to do this with a fetchResultsController, even though it would be trivial without one. As so often follows my questions on SO – what am I missing here?
To stop updates to the table view, you just have to set the fetched results controllers delegate to
nil:To resume updates, you set the delegate again, perform an initial fetch, and reload the table data:
performFetchis necessary because the fetched results controller does not track changes to the managed object context while it has no delegate.Alternatively, you could also add the data in a background thread, using a second managed object context and save that context only when all data is imported.
The main thread could register for the
NSManagedObjectContextDidSaveNotificationof the background thread and callmergeChangesFromContextDidSaveNotification:to refresh the objects.In this case, no changes to the fetched results controller are necessary.