Newly created NSManagedObject returns temporary objectID even after save

前端 未结 2 1302
北海茫月
北海茫月 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.

提交回复
热议问题