Strange behavior when using child/parent NSManagedObjectContext

不想你离开。 提交于 2019-12-03 08:32:28

My solution was to:

1) Use the background MOC as the parent MOC and the main MOC as a child. As a bonus I don't need to save the main MOC to get the permanent IDs.

[DC.backgroundMOC performBlock:^{
    // Add, save and update managed objects
    [DC saveContext]; // The changes is being pushed to the main context
}];

2) Use NSManagedObjectContextDidSaveNotification to keep the main MOC up to date (the main MOC is updating the UI)

- (void) backgroundMOCSaved:(NSNotification*)notification {
    [mainMOC performBlock:^{
        [mainMOC mergeChangesFromContextDidSaveNotification:notification];
    }];
}

I had this problem and the solution was making sure all operations on the parent MOC are done with performBlock:, even the initial setup:

    parentManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    [parentManagedObjectContext performBlock:^{
        [parentManagedObjectContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
        [parentManagedObjectContext setPersistentStoreCoordinator:coordinator];
    }];

Once I did this, my child MOCs started picking up changes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!