Search code examples
cocoacore-datansmanagedobjectcontext

Child Moc not getting recent changes when fetching


I have a 1 parent 2 child moc situation. 1 of the childs is the main interface moc and the other one is a private queue for sync changes on the cloud.

I've run into a situation where the private cloud sync child mod saves changes, the parent can see the changes but the other interface child moc does not.

I have an entity called Team, that has a to-many relationship field called TeamMembers. After saving on the private queue, I fetch the Team Entity on the parent moc and get all changes correctly. After that, I fetch the interface child moc and do not get the changes. If I create another child moc, I do get the changes.

Any ideas ?


Solution

  • I experienced the same issue because it seems that any cached objects in the interface context do not get updated by the parent context automatically. To handle this, I added an observer to the NSManagedObjectContextDidSaveNotification like this:

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(contextDidSave:)
                                                 name:NSManagedObjectContextDidSaveNotification
                                                object:parentContext];
    

    and merged the changes saved to the parent context into the default context manually:

    - (void)contextDidSave:(NSNotification *)notification {
        SEL selector = @selector(mergeChangesFromContextDidSaveNotification:); 
        [interfaceContext performSelectorOnMainThread:selector withObject:notification waitUntilDone:YES];
    }
    

    This seems to be the standard solution to this issue from what I have seen (ie. Core Data merge two Managed Object Context)