I have a UITableView
that fetches data from CoreData
using FetchedResultsController
and it registers for the data update.
On a second thread, I download the data from the server and update the same data (that's used by the UITableView
). The update is not complicated and It is just updating a BOOL field of the entity.
When I call the save on Object Context, I get this exception: NSInternalInconsistencyException
and the reason is
"Failed to process pending changes before save. The context is still dirty after 100 attempts. ..."
If I do not save right after开发者_如何学C the update but only at the time when the application is about to terminate, the application runs fine and the UITableView
is correctly updated and the data is persisted.
Any pointer on why that might be happening? Am I doing something wrong?
Managed object contexts are not thread safe. Do you have a separate MOC
for each thread?
If so, I believe the correct pattern is to register for NSManagedObjectDidSaveNotifications
from the background MOC
such that you can do a mergeChangesFromContextDidSaveNotification
on the main MOC
(from the main thread). This will keep your MOCs
in sync; it does not happen automatically.
精彩评论