Newly created NSManagedObject returns temporary objectID even after save

前端 未结 2 1243
北海茫月
北海茫月 2021-02-07 09:39

Very simple situation. Not sure why it\'s causing an issue.

I have a view that creates a new NSManagedObject in a child NSManagedObjectContext. When the user presses \"

相关标签:
2条回答
  • 2021-02-07 10:11

    Because the child does not get updated back from the parent MOC. The parent MOC will update its own instance of the NSManagedObject with a permanent ID but that change will not be pushed down to the instance of that NSManagedObject belonging to the child MOC.

    Update 1

    I do not use -objectID in this situation. It has some uses but it is not a permanent uniqueID. In a situation like this, I prefer to add my own uniqueID to the entity and then fetch it from the main context.

    You could also just listen for the context save notifications or use an NSFetchedResultsController which will receive updates.

    0 讨论(0)
  • 2021-02-07 10:15

    Have you tried obtaining the permanent ID's for your objects? I've had success sharing NSManagedObjectID's between contexts by calling

    NSError *error = nil;
    [self.context obtainPermanentIDsForObjects:@[object1, object2]
                                         error:&error];
    

    followed by my save method. I structure my contexts in such a way that there is an I/O context which is the parent to my 'read context' (context used for my main thread activity) which is the parent to my background task contexts. Save in this case propagates all the way up the chain.

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