Thanks in advance for any help. I fought this today and I think there is something seriously wrong with my understanding of how the structure works.
I am working on a main data application where entities have parent / child relationships. At startup, the application creates an NSManagedObjectContext (MOC). When the application is launched for the first time, it uses an asynchronous block to import the contents of the plist into the second MOC (the root of the node is obtained from the main MOC using the URI and -managedObjectIDForURIRepresentation :), immediately before the block, it stores the second context.
In my data controller, I subscribe to NSManagedObjectContextDidSaveNotification, and the following code runs when a notification is sent:
- (void)backgroundContextDidSave:(NSNotification *)notification {
if(![notification.object isEqual:self.managedObjectContext]){
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:@selector(backgroundContextDidSave:)
withObject:notification
waitUntilDone:NO];
return;
}
[self.managedObjectContext mergeChangesFromContextDidSaveNotification:notification]; ;
}
}
I did a test of the health of this code and, of course, when the second MOC saves, it is called from the thread executing the block, then it is deferred and launched from the main thread. The notification object contains all the objects imported into the second MOC, including two that we will deal with in the following.
When this is over, I run the following code, which is in a subclass of NSManagedObject, to which the objects belong, it is just designed to remove a child from its parent:
TreeEntry *oldParent=self.parent;
NSManagedObjectContext *selfContext=self.managedObjectContext;
NSManagedObjectContext *parentContext=self.parent.managedObjectContext;
NSManagedObjectContext *sharedContext=[[DataController sharedDataController] managedObjectContext];
assert([selfContext isEqual:parentContext] && [selfContext isEqual:sharedContext]);
[sharedContext refreshObject:self.parent mergeChanges:NO];
[sharedContext refreshObject:self mergeChanges:NO];
[sharedContext saveChanges];
self.parent=nil;
[oldParent removeChild:self];
[sharedContext saveChanges];
The last crash persists with error 133020 Cocoa, which is a merge merge. Two NSMergeConflicts errors in error refer to the records we are dealing with (self and self.parent).
, . , . , , , . ? , .
, , , , .
? , , , , !