I am having a crash problem when using a TableView with Core Data and any help is greatly appreciated. The scenario is:
I have a UITableViewController showing data stored in Core Data. I use a NSFetchResultsController to do the fetching as instructed by the documentation. I have a dedicated NSManagedObjectContext exclusive for the main thread to fetch the data.
The data is actually coming from a server. When my application launches, I have a background thread to refresh the data into my Core Data stack. As recommended by Apple, I use a different NSManagedObjectContext in the background thread to refresh the data. During the refresh, old data will be deleted.
After the background saves the changes, I used the NSManagedObjectContextDidSaveNotification to trigger a call to do the mergeChangesFromContextDidSaveNotification on the main thread's context.
controllerDidChangeContent of the FRC is also implemented which call the UITableViewController to reload.
Everything works fine - except if I scroll the TableView while the data refresh is in progress, the app will crash with the "Core Data could not fulfill a fault..." error. After tracing through the code, I believe the cause is there is a small time lag between the background thread saving the delete of the data and the main thread context merge operation. During this time lag, some of the managed objects in the main thread's context is deleted and so when the table is scrolled and the data source method access the deleted object, the app will crash.
Is my belief correct? If so, how should I deal with this time lag?
Many Thanks.