9
votes

I am inserting a new Folder entity into my model object context (MOC), that is a child of the master MOC. Before saving, [[newFolder objectID] isTemporaryId] returns YES. When I save first my MOC and then the master MOC and ask the [[newFolder objectID] isTemporaryId], it again says YES. I expected it to now be a persisted object, and indeed, if I interrogate the SQLite file, it contains the new folder. For the entire time of my running application, it will say YES. Not before I quit and restart, thus loading the object up from the store, does it say NO.

I always thought a NSManagedObject changed from a temporary object to a stored object with no longer a temporary objectID when it was saved. When does an object get a objectID that is not temporary?

Cheers

Nik

1
I should probably add that I cannot use "NSManagedObject *obj = [MOC existingObjectWithID:oid error:&error];" where oid is the (still temporary) ObjectID of the newly inserted & saved folder - niklassaers
So far, the only useful way out of this is to follow up a save-to-disk with [self.masterMOC reset]; - niklassaers

1 Answers

13
votes

Objects in child contexts aren't altered by changes in the parent context until the objects are refreshed. Perhaps refreshing the object after saving the parent context will do the trick.

If that doesn't work, there's always -[NSManagedObjectContext obtainPermanentIDsForObjects:error:].