Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

BadWolf13

macrumors 6502
Original poster
Dec 17, 2009
271
0
Ok, so I've got a core data app here, and the way it's set up, is that the different windows each have their own managed object context, but are all using the same persistent store coordinator. In the main window, I've got the core data widget, just for easy viewing of the state of my persistent store. The thing I don't understand, is that when window #2 saves a few objects into my persistent store, and I then click on the fetch button in the widget in the main window, it won't load all the objects that are just saved. Now when I quit the app and restart it, the widget does load all the objects that were saved.

I would have thought that it would load simply because both windows are using the same persistent store coordinator, but I clearly don't understand Core Data that well. Can someone explain why this is happening the way it is?
 
I dusted off cdcli from the core data utility tutorial.

I modified it so that it creates 2 managed object contexts backed onto the same persistent store coordinator. I had it insert a new Run into the first context and then had it fetch all the Runs from the second context. The second context fetch the new Run.

I played around with having the context pre-fetch Runs or not in different combinations. This didn't matter, the second context always fetched the new Run.

The only way I could get the second context to NOT fetch the new Run was to have it backed by different persistent store coordinator instance to the first context.

Perhaps log the persistentStoreCoordinator of each of your contexts just to be absolutely sure they're backed by the same instance.
 
It's a good thought, but what I'm doing is passing the persistent store coordinator as an argument in the initializer method, and setting the internal coordinator to the same.
 
Ok, so I tried what you suggested anyways, and yeah, it's all the same persistent store coordinator. In researching this further, I don't think it's the fact that I'm using two mocs here, I think it comes down to the function of the core data widget. You see, I have another method that uses the same moc as the core data widget to add objects and save them immediately. Clicking Fetch still doesn't bring them up.

Here's the question I have, does the fetch: command in NSObjectController cause the moc to fetch from the persistent store, or does it just reload from the moc?
 
NSManagedObjectContext has a function that allows you to update it when another NSManagedObjectContext saves. What if you try something like this when you assign your NSManagedObjectContext?

Code:
 [[NSNotificationCenter defaultCenter] addObserver:self.managedObjectContext
                                          selector:@selector(mergeChangesFromContextDidSaveNotification:) 
                                              name:NSManagedObjectContextDidSaveNotification 
                                            object:nil];

Other than that, I'm not super familiar with NSManagedObjectController but in iOS with NSFetchedObjectController I know that it is relatively easy to mess up threaded NSManagedObjectContexts such that it will cache and/or fetch stale data. The above "usually" works for me in those cases. Getting multiple NSManagedObjectContexts working properly in conjunction I've found to be a pretty tricky thing, there are just so many paths that can get jumbled up it can be hard to disentangle what is actually going on.
 
I'm not really sure what an NSManagedObjectController is, I'm using an NSArrayController. It's created automatically with the Core Data Widget.

Thanks for the idea of the merging, that might help, but I would still like to know why the fetch: isn't fetching. I mean, as I understand, once the one moc saves the data to the persistent store, that information should be there to be accessed by any moc in any class(or any program for that matter). Am I wrong?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.