views:

85

answers:

1

I have an NSWindow which I use to create new records. After pressing the Add button, a certain method is called in which I do the following:

- (IBAction)addActionAddSheet:sender {
 NSManagedObjectContext *moc = [self managedObjectContext];
 NSManagedObject *newObject  = [NSEntityDescription insertNewObjectForEntityForName:@"Recipe" inManagedObjectContext:moc];

 [newObject setValue:[newRecipeName stringValue] forKey:@"name"];
 [newObject setValue:[newRecipeInstructions string] forKey:@"instructions"];

 NSLog(@"New object is: %@", newObject);

 [NSApp endSheet:addSheet];
 [addSheet orderOut:sender];
}

The (edited) output from NSLog after I have executed this twice, is as follows:

<NSManagedObject: 0x10045c6a0> (entity: Recipe; id: 0x1004564a0 <x-coredata:///Recipe/t18FEC674-8937-49DF-A18B-940EF82E83C32> ; data: {
    instructions = X;
    name = X;
})
<NSManagedObject: 0x1149069a0> (entity: Recipe; id: 0x100106a20 <x-coredata:///Recipe/t18FEC674-8937-49DF-A18B-940EF82E83C33> ; data: {
    instructions = Y;
    name = Y;
})

This is completely as expected. However, my tableview, which is bound to an array controller, shows that both objects X and Y have X as value for instructions.

When I exit my application and the data is saved, what I see in my xml file is again different:

    <attribute name="name" type="string">Y</attribute>
    <attribute name="instructions" type="string">Y</attribute>

    <attribute name="name" type="string">X</attribute>
    <attribute name="instructions" type="string">Y</attribute>

This time they both have value Y.

The output from NSLog does show that I'm dealing with two different objects here, so I'm not sure what I might be doing that could influence both.

These are is my unedited output:

2009-12-07 14:46:18.409 Recipe[11578:a0f] managedObjectContext
2009-12-07 14:46:18.411 Recipe[11578:a0f] persistentStoreCoordinator
2009-12-07 14:46:18.411 Recipe[11578:a0f] managedObjectModel
2009-12-07 14:46:18.423 Recipe[11578:a0f] applicationSupportDirectory
2009-12-07 14:46:18.436 Recipe[11578:a0f] externalRecordsDirectory
2009-12-07 14:46:20.484 Recipe[11578:a0f] Show Add sheet
2009-12-07 14:46:20.485 Recipe[11578:a0f] Clear values
2009-12-07 14:46:20.494 Recipe[11578:a0f] call beginSheet
2009-12-07 14:46:23.632 Recipe[11578:a0f] addActionAddSheet: -- Add button clicked
2009-12-07 14:46:23.632 Recipe[11578:a0f] Create new mananged obj in context
2009-12-07 14:46:23.634 Recipe[11578:a0f] managedObjectContext
2009-12-07 14:46:23.635 Recipe[11578:a0f] Set values for name and instructions
2009-12-07 14:46:23.636 Recipe[11578:a0f] New object is: <NSManagedObject: 0x1001c89c0> (entity: Recipe; id: 0x1001c9470 <x-coredata:///Recipe/tE9BD4EE3-082C-4715-AB66-2C3580223F9E2> ; data: {
    instructions = A;
    name = A;
})
2009-12-07 14:46:23.636 Recipe[11578:a0f] call [NSApp endSheet:] and [addSheet orderOut:]
2009-12-07 14:46:23.637 Recipe[11578:a0f] addSheetDidEnd:returnCode:contextInfo: -- empty method
2009-12-07 14:46:35.327 Recipe[11578:a0f] Show Add sheet
2009-12-07 14:46:35.328 Recipe[11578:a0f] Clear values
2009-12-07 14:46:35.337 Recipe[11578:a0f] call beginSheet
2009-12-07 14:46:39.836 Recipe[11578:a0f] addActionAddSheet: -- Add button clicked
2009-12-07 14:46:39.836 Recipe[11578:a0f] Create new mananged obj in context
2009-12-07 14:46:39.838 Recipe[11578:a0f] managedObjectContext
2009-12-07 14:46:39.839 Recipe[11578:a0f] Set values for name and instructions
2009-12-07 14:46:39.843 Recipe[11578:a0f] New object is: <NSManagedObject: 0x102070ad0> (entity: Recipe; id: 0x10205e420 <x-coredata:///Recipe/tE9BD4EE3-082C-4715-AB66-2C3580223F9E3> ; data: {
    instructions = B;
    name = B;
})
2009-12-07 14:46:39.843 Recipe[11578:a0f] call [NSApp endSheet:] and [addSheet orderOut:]
2009-12-07 14:46:39.844 Recipe[11578:a0f] addSheetDidEnd:returnCode:contextInfo: -- empty method

Recipe A:

Recipe B:

My array controller:

My table column's bindings:

My label's binding:

+2  A: 

How are you binding your table / columns to the array controller? It sounds as though your Instructions column isn't bound to arrangedObjects.instructions, but possibly selection.instructions.

Joshua Nozzi
My NSArrayController is bound to the Entity named Recipe.My TableColumns are bound to the key paths of Recipe's arrangedObjects.My detail view objects (textfield and textview) are bound to selection.name and selection.instructions.When I add the second Recipe, the tableview shows that both recipe A and recipe B have 'B' as instructions, while the detail view still shows that they have 'A' as instructions.
nephilim
You may need to take a screenshot of both the bindings settings for the column and the detail label and post them. I'd be very surprised if the problem weren't there.
Joshua Nozzi
I've added screenshots for the array controller and the bindings. I've only included the value binding since the others aren't bound to anything.
nephilim
That's quite strange. Is your NSTableView instance itself bound in any way to the array controller? NSTableView has its own bindings. There might be something weird going on with the selection management.
Joshua Nozzi
Also, it can often be helpful to completely "rip out the plumbing" (delete the array controller and table view and recreate both). Sometimes "weird stuff" just happens.
Joshua Nozzi
I tried this, but this didn't solve my problem either. I've now created a whole new project and set up my datamodel and nib. I copied my insert code from the other project and now it seems to work, strangely enough. I'll now add bit by bit so I have a better idea where it goes wrong, if it does again.
nephilim