I spent few hours trying to solve problems with parent-child core data model. But let's start from the beginning. I've got main context and child context which has .parentContext
as the main context.
- (NSManagedObjectContext *)mainContext {
if (!_mainContext) {
_mainContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_mainContext setPersistentStoreCoordinator:self.persistentStoreCoordinator];
}
return _mainContext;
}
- (NSManagedObjectContext *)childContext {
if (!_childContext) {
_childContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_childContext setParentContext:self.mainContext];
}
return _childContext;
}
Next I create two objects in separated contexts.
Entity2 *entity2 = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([Entity2 class])
inManagedObjectContext:_stack.childContext];
Entity1 *entity1 = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([Entity1 class])
inManagedObjectContext:_stack.mainContext];
entity1.arg1 = @"ABC";
entity1.arg2 = @"DEF";
Here all is ok.
Then I want to add one object to relationship of another, no matter if I try to add entity1
to entity2
or vice versa.
[entity1 setEntity2:entity2];
In article of Multi-Context Core Data | Cocoanetics I read that I should perform block with save operation because object's don't know each other. So it look like this:
[_stack.childContext performBlock:^{
NSError *error;
if (![_stack.childContext save:&error]) {
NSLog(@"Error: %@", error);
}
[_stack.mainContext performBlock:^{
NSError *error;
if (![_stack.mainContext save:&error]) {
NSLog(@"Error: %@", error);
}
[entity1 setEntity2:entity2];
}];
}];
But wherever I put [entity1 setEntity2:entity2]
It ends with:
2014-06-22 08:48:24.444 MultiContextualCoreData[1324:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Illegal attempt to establish a relationship 'entity1' between objects in different contexts (source = <Entity2: 0x8c81570> (entity: Entity2; id: 0x8c80000 <x-coredata:///Entity2/tC31696AE-0A0A-424E-9C8E-1E19085F3FB43> ; data: {
entity1 = nil;
}) , destination = <Entity1: 0x8c80200> (entity: Entity1; id: 0x8c80250 <x-coredata:///Entity1/tC31696AE-0A0A-424E-9C8E-1E19085F3FB42> ; data: {
arg1 = ABC;
arg2 = DEF;
entity2 = nil;
}))'
*** First throw call stack:
(
0 CoreFoundation 0x01b541e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x018d38e5 objc_exception_throw + 44
2 CoreData 0x00293b7e _PFManagedObject_coerceValueForKeyWithDescription + 3614
3 CoreData 0x002614e9 _sharedIMPL_setvfk_core + 185
4 CoreData 0x0027ada7 _svfk_0 + 39
5 MultiContextualCoreData 0x00002d4a -[AppDelegate test] + 554
6 MultiContextualCoreData 0x00002acd -[AppDelegate application:didFinishLaunchingWithOptions:] + 605
7 UIKit 0x0058d14f -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 309
8 UIKit 0x0058daa1 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1810
9 UIKit 0x00592667 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
10 UIKit 0x005a6f92 -[UIApplication handleEvent:withNewEvent:] + 3517
11 UIKit 0x005a7555 -[UIApplication sendEvent:] + 85
12 UIKit 0x00594250 _UIApplicationHandleEvent + 683
13 GraphicsServices 0x03a07f02 _PurpleEventCallback + 776
14 GraphicsServices 0x03a07a0d PurpleEventCallback + 46
15 CoreFoundation 0x01acfca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
16 CoreFoundation 0x01acf9db __CFRunLoopDoSource1 + 523
17 CoreFoundation 0x01afa68c __CFRunLoopRun + 2156
18 CoreFoundation 0x01af99d3 CFRunLoopRunSpecific + 467
19 CoreFoundation 0x01af97eb CFRunLoopRunInMode + 123
20 UIKit 0x00591d9c -[UIApplication _run] + 840
21 UIKit 0x00593f9b UIApplicationMain + 1225
22 MultiContextualCoreData 0x00002efd main + 141
23 libdyld.dylib 0x0219b701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
What i did wrong here?
Thank you for help in advance.
The issue is that two objects have to belong to the same context in order to establish relationships between them. In your case, one of the contexts has a private type, it has its own queue to work at, where the main context works on the main queue. This is a way to protect you from running into concurrency problems down the road.
Possible solution are
entity1
, save the main context, retrieve it in the child context using managedObjectID
or a fetch request if it makes more sense and only then establish relationship when you have entity1
and entity2
in the same contextUser contributions licensed under CC BY-SA 3.0