views:

322

answers:

1

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 object 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?

+4  A: 

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.

Marcus S. Zarra
That's the answer I needed to hear. For some reason I'd convinced myself (at 2am) that I needed another managed object context. I was just being greedy!Thanks as always, Marcus!
Tony Arnold