0

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.

Mundi
  • 79,884
  • 17
  • 117
  • 140
schmudu
  • 2,111
  • 1
  • 21
  • 30

2 Answers2

0

It looks like your value attribute is somehow not correctly defined. Here is a check list:

  1. You already checked the data type in the data model
  2. You should also check the NSManagedObject subclass for the right types.
  3. Also, check again how you set the new value.
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • If this does not help, you need to post some code (implied by the above). – Mundi Jan 15 '13 at 11:17
  • Hi Mundi, thanks for your response. I checked 1. I double-checked 2 and value is set to NSString*. One thing that confuses me is why would the child context be set correctly while the root context does not. According to the print out in the original post the child context has the right value of 4 however even after I call [_childContext save:&error] the rootContext shows a value of nil. I suspect that because the value is nil it doesn't write the attribute out to the xml file that I'm inspecting. Just a hunch though... – schmudu Jan 15 '13 at 13:43
0

Hmm....still got lots to learn about CoreData.

Basically the problem I encountered was that I was:

  1. Creating a NSManagedObject, but not inserting into the context.
  2. Updating the attributes of the newly created object.
  3. Inserting the object into the context.

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.

schmudu
  • 2,111
  • 1
  • 21
  • 30