I am observing a weird bug in iOS using core-data.
Basically, my setup is as follows-
Have 2 NSManagedObjectContexts, tempContext and mainContext.
I use tempContext as a temporary ‘scratchpad’ for creating and editing entities, and when I want to save them, I move it to the mainContext to save.
Note that mainContext is not the parent of tempContext.
The move happens as follows-
[1] newEntity.property1 = @"SomeProperty";
newEntity.managedObjectContext // this is tempContext currently
[2] saveEntityInTempContext
[3] newEntity = (Entity *)[mainContext objectWithID:newEntity.objectID];
[4] [mainContext insertObject:newEntity];
Now, after insert[4], if I check newEntity.property1, it is set as nil.
However, if I check the property first after [3] and then after [4], it shows it correctly as “SomeProperty”. I don’t understand how forcing faulting should make a difference here.
My goal is just to move an entity from one context to another.
I’m not sure what the immediate cause is, but it’s almost certainly a weird side effect of an unnecessary step that really doesn’t make any sense. I’m kind of surprised you don’t get an exception in the last step.
Specifically, step #4 is completely unnecessary. Once you finish step #3, you have an instance that was loaded from
mainContext. You don’t need to do anything more at that point. It doesn’t make sense to callinsertObjectfor an object that you just looked up from the same managed object context.Assuming that
tempContextandmainContextuse the same persistent store coordinator, the life cycle of this object proceeds as follows in your steps:newEntityis already created but not saved.tempContextknows aboutnewEntitybut the data store does not (and of coursemainContextdoesn’t know about it).newEntity.newEntity‘s object ID changes to a permanent value.objectWithID:call, the instance ofnewEntitygets replaced with a different instance that was looked up viamainContext. It’s attributes match the ones you created in step 1 and saved to the data store in step 2, because they were loaded from the data store.insertObjectserves no purpose– because the newnewEntityyou got in step 3 was looked up frommainContext, meaning thatmainContextalready knows about it. This is where I would have expected an exception. No exception occurs, but the call still doesn’t make sense.So, I’d just drop step #4 and be done with it.