How can I keep multiple NSManagedContext's in sync?
I've spent a couple of hours tonight trying to create a setup whereby I have three managed object contexts - one attached to my application's delegate, one in an NSObject
subclass responsible for UITableView
updates (via an NSFetchedResultsController
) and one in an NSOperation
subclass.
I can get the changes from the NSOperation
subclass to the app delegate to flow through just fine by observing NSManagedObjectContextDidSaveNotification
, and firing off mergeChangesFromContextDidSaveNotification:
on my main thread, but when I try to do the same thing from my UITableView
data source, I get the following exception:
2010-02-19 02:00:39.750 MyApp[44687:207] Serious application error. Exception was caught during Core Data change processing: *** -[NSCFArray initWithObjects:count:]: attempt to insert nil object at objects[0] with userInfo (null)
2010-02-19 02:00:39.750 MyApp[44687:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFArray initWithObjects:count:]: attempt to insert nil 开发者_如何学JAVAobject at objects[0]'
I'd appreciate any advice - all of my previous designs involving Core Data managed object contexts have been very simple 1:1 sync scenarios. Is what I'm proposing even possible?
A better question is WHY are you trying to keep multiple contexts in sync. Having two, one for your main application usage and one for imports makes sense but the imports context does not need to be kept in sync and you already know how to feed your main context off the import context.
So what is that third context for? IF it is just for table view updates, that is a bad design and you should be using a single context for the entire UI layer. That is most likely the correct solution to your problem instead of trying to keep three contexts juggling in the air.
精彩评论