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

后端 未结 6 1021
傲寒
傲寒 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:12

    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.

    0 讨论(0)
  • 2020-12-23 14:16
    book *book = [mainContext ........]   //Get book from default context
    
    NSManagedObjectID *objectId = [book objectID];
    
    Book *tmpBook = [tmpContext objectWithID:objectId]; //Now book has the legal relationship
    
    0 讨论(0)
  • 2020-12-23 14:18

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

    0 讨论(0)
  • 2020-12-23 14:28

    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!

    0 讨论(0)
  • 2020-12-23 14:32

    Swift version...

    context.insert(objectFromOtherContext)
    
    0 讨论(0)
  • 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 :)

    0 讨论(0)
提交回复
热议问题