So I have two NSManagedObjectContext objects. A parent context and a child context. I have a few NSManagedObjectModels and I'm able to create/edit/destroy 4 out of 5 of them. No problem. I can insert objects on the child context then save it and voila it pops up on the root context.
However it's the 1 out of 5 that's giving me trouble. I can insert it with no problem, it'll show up on root context. When I attempt to change the attributes of the object though it does not get updated on the root context. Here's the printout from the console (notice the value attribute does not get updated).
====root_context:("<Token: 0x1006f6a30> (
entity: Token;
id: 0x107115130 <x-coredata:///Token/t2AC116F0-E89B-485D-B0A9-C3D2A58B84847> ;
data: { association = 0;
equation = \"0x107114b10 <x-coredata:///Equation/t2AC116F0-E89B-485D-B0A9-C3D2A58B84846>\";
isValid = 0;
precedence = 0;
type = 0;
value = nil;})")
====child_context:("<Token: 0x1071150a0> (
entity: Token;
id: 0x107115130 <x-coredata:///Token/t2AC116F0-E89B-485D-B0A9-C3D2A58B84847> ;
data: { association = 0;
equation = \"0x107114b10 <x-coredata:///Equation/t2AC116F0-E89B-485D-B0A9-C3D2A58B84846>\";
isValid = 0;
precedence = 0;
type = 0;
value = 4;})"
)
From my newbie eyes these seem to be the same object. Even when I print out the objectID for both of these objects (even though they're in different contexts) their objectID matches.
Some more background
To merge I'm calling [_childContext save:&error]
which pushes the changes to the parent context. This code is being called every time it detects a change in the child context. And then once I observe a NSManagedObjectContextDidSaveNotification in the child context I call [_rootContext mergeChangesFromContextDidSaveNotification:notification]
Additional Note
I don't know if this is another clue, but when I save the data to an XML file it omits the value
attribute. Here's the output:
<object type="TOKEN" id="z104">
<attribute name="type" type="int16">0</attribute>
<attribute name="precedence" type="int16">0</attribute>
<attribute name="isvalid" type="bool">0</attribute>
<attribute name="association" type="int16">0</attribute>
<relationship name="equation" type="1/1" destination="EQUATION" idrefs="z106"></relationship>
</object>
Thanks everyone!
p.s. I checked my .xcdatamodeld file and everything seems to be in order. I'm storing value as a String and the rest of them as Integer 16 and Booleans.
Hmm....still got lots to learn about CoreData.
Basically the problem I encountered was that I was:
However if I reverse steps 1 and 2 and instead insert the object into the context first and then update the attributes it works just fine.