Illegal attempt to establish a relationship 'xyz' between objects in different contexts

后端 未结 6 1020
傲寒
傲寒 2020-12-23 13:44

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

6条回答
  •  时光说笑
    2020-12-23 14:35

    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 :)

提交回复
热议问题