I am using Apple\'s CoreDataBooks
sample application as a basis for pulling data into a secondary managed object context in the background, and then merging tha
You can't have relationships between objects in different managed object contexts. So one way of getting around that is to bring the object into the managed object context.
For example:
NSManagedObject *book = // get a book in one MOC
NSManagedObject *owner = // get an owner in a different MOC
[[owner mutableSetValueForKey:@"books"] addObject:[owner.managedObjectContext objectWithID:[book objectID]]];
So what you're doing is actually fetching the Book
into the same managed object context with owner
. Keep in mind, though, that this is only possible if book
has already been saved. The managed object context is going to look for the object in the persistent store, so it has to be saved first.
book *book = [mainContext ........] //Get book from default context
NSManagedObjectID *objectId = [book objectID];
Book *tmpBook = [tmpContext objectWithID:objectId]; //Now book has the legal relationship
As the error says, you're not allowed to have a relationship in one Core Data object whose value is set to another object held in a different context. One way you can get around this is to wait until after you save the new object and merge it back into the primary context, then set the owner
relationship as appropriate (since both objects are now in the same context, there's no problem with that).
Here, you are trying to establish a relation between two objects which are fetched/created with a different context. Core data won't allow you to establish such relations. To achieve this, you should fetch the second object (you are trying to make a relationship with) with the context of the first object with objectID. Now you should be able to establish a relation between these two objects. For example:
MagicalRecord.saveWithBlock({[unowned self] (localContext: NSManagedObjectContext!) in
let user = User.MR_createEntityInContext(localContext)!
.....
.....
}) //here local context stores data on end of block itself
MagicalRecord.saveWithBlock({[unowned self] (localContext: NSManagedObjectContext!) in
let address = Address.MR_createEntityInContext(localContext)!
.....
.....
let user = localContext.objectWithID(self.user!.objectID) as! User
user.address = address
})
Hope this will help you!
Swift version...
context.insert(objectFromOtherContext)
I had the same problem and this sentence helped me to solve the error.
You can't have relationships between objects in different managed object contexts. So one way of getting around that is to bring the object into the managed object context.
Here's my code (I replaced the variables to make it work for you):
// Have the owner object and get the managedObjectContext
Owner *owner = [[DataFunctions alloc] getCurrentOwner];
NSManagedObjectContext *context = [owner managedObjectContext];
// Create a book and use the manageObjectContext of owner
Book *book = [NSEntityDescription insertNewObjectForEntityForName:@"Book" inManagedObjectContext:context];
[newQuote setValue: "Book Title" forKey:@"title"];
[newQuote setOwner:owner];
I hope this helps :)