CoreData: “Dangling reference to an invalid object.” error

后端 未结 9 2052
余生分开走
余生分开走 2021-01-01 11:44

I\'m working on a Cocoa-Touch app, it uses CoreData and has some NSPersistentObject subclasses generated by the XCode model editor.

I\'ve noticed that recently, when

相关标签:
9条回答
  • 2021-01-01 12:25

    This error usually arises because a relationship is set improperly often when an object is left without a necessary reciprocal relationship. The object is "dangling" because the object graph says it should be in a relationship but it is just hanging off in space unconnected to any other object. The object is still valid in the sense that it is internal consistent but it's not in a valid place in the graph.

    0 讨论(0)
  • 2021-01-01 12:25

    I have another example of how to cause this problem: I have a MOC with a concurrency type of NSMainQueueConcurrencyType. Somewhere in the code I do this:

    __block MyObjectType1 *obj1;
    [managedObjectContext performBlockAndWait:^{
        obj1 = [NSEntityDescription insertNewObjectForEntityForName:@"Thing" inManagedObjectContext:managedObjectContext];
    }];
    // some other stuff
    [self saveContext];
    __block NSManagedObjectID *object1ID;
    [managedObjectContext performBlockAndWait:^{
        object1ID = [obj1 objectID];
    }];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // do some slow-ish stuff
        [managedObjectContext performBlockAndWait:^{
            // create new object that has a relationship
            NSManagedObject *obj1_copy = [managedObjectContext objectWithID:object1ID];
            MyObjectType2 *obj2 = [NSEntityDescription insertNewObjectForEntityForName:@"OtherThing" inManagedObjectContext:managedObjectContext];
            obj2.relatedThing = obj1_copy;
        }];
        [self saveContext];
    });
    

    It turns out that sometimes, this fails. I still don't understand why, but forcing to get a non-temporary objectID seems to do the trick:

    [context performBlockAndWait:^{
        NSError *error;
        [managedObjectContext obtainPermanentIDsForObjects:@[obj1] error:&error];
        object1ID = obj1.objectID;
    }];
    
    0 讨论(0)
  • 2021-01-01 12:26

    Adding to original answer, there can be couple of reasons for this crash to occur. Read the error description carefully, It In my case i was setting up a relationship with object from another context.

    0 讨论(0)
  • 2021-01-01 12:36

    My problem was solved using this code:

    [[CustomManagedObject managedObjectContext] performBlockAndWait:^{
            NSError *error;
            if (![[CustomManagedObject managedObjectContext] save:&error])
            {
                NSLog(@"Error in Saving: %@", [error.userInfo description]);
            }
        }];
    
    0 讨论(0)
  • 2021-01-01 12:42

    I know it's long after the fact, but I've been fighting this problem on a Core Data Model that has ALL relationships set to Nullify. Kept getting these dangling references until I found a single setPrimitiveValue instead of setValue when I was adding to the relationships. Be careful, with relationships, you gotta be sure you do the right thing to let Core Data maintain the relationships for you!

    0 讨论(0)
  • 2021-01-01 12:47

    This question was asked a while back, but I just ran into it. It was not due in my case to a improperly set relationship technically. It was due to the object being set created in a different context, note not on a different thread just a different context on the same thread.

    So look for threading issues if you are doing anything with thread with Core Data.

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