Multiple Managed Object Contexts and Not Fetching Properly

Discussion in 'Mac Programming' started by BadWolf13, Mar 13, 2011.

  1. BadWolf13 macrumors 6502

    Joined:
    Dec 17, 2009
    #1
    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?
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    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.
     
  3. BadWolf13 thread starter macrumors 6502

    Joined:
    Dec 17, 2009
    #3
    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.
     
  4. BadWolf13 thread starter macrumors 6502

    Joined:
    Dec 17, 2009
    #4
    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?
     
  5. seepel macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #5
    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.
     
  6. BadWolf13 thread starter macrumors 6502

    Joined:
    Dec 17, 2009
    #6
    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?
     

Share This Page