View Full Version : NSTableView + bindings + core data
djohnson
2006.03.03, 03:16 PM
My next problem that I am banging my head on my desk is about how NSTableView responds to Core Data and Bindings. It seems that when you add a new entry into your array, it does not always append to the end of the array. It will stay correctly until you close the application and reopen it. The data for the row will be together but the rows will all be arranged differently. You can close it and reopen it, without editing or adding anything, and the data will be rearranged again! This behavior makes no sense to me, nor does it make sense to my diligent google searches. Surely someone has ran across this before and has some input. Thanks!
BTW - I am compiling a list of oddities when using Core Data + Bindings and will soon be publishing them on my website. If you have some to add, feel free to PM me.
Renaud
2006.03.03, 03:48 PM
It is not an odditiy. CoreData doesn't care about the insertion order. if you need a specific order, you have to provide sort criteria: in your case an attribute which will be incremented each time a new object is created. A method (which needs some improvements) is listed here (http://developer.apple.com/documentation/Cocoa/Conceptual/NSPersistentDocumentTutorial/03_CustomClass/chapter_4_section_4.html#//apple_ref/doc/uid/TP40002831-DontLinkElementID_21)
djohnson
2006.03.03, 08:10 PM
That would be why that data changes in the table everytime the app is launched... What an odd way for a data management sytem to work, though I guess it is the same as most other databases.
Anyways, guess I do need a key item the same as I do for SQL tables. Odd how examples never seem to include this bit of information... Guess it is another item to add to my growing collection.
djohnson
2006.03.06, 11:06 AM
If people are interested in adding a unique id field to a NSTableView, here is what I did:
First, create a field that may or may not show up in the NSTableView. I called mine id and created it in the Data Model.
Next, you will need to create a custom NSManagedObject class so that you can overwrite the -(void) awakeFromInsert function. Make sure you change the super class to NSManagedObject from NSObject. Also, change the class for the Entity in the data model to your new class. Add the following function:
- (void) awakeFromInsert
{
[super awakeFromInsert];
NSDate *date = [NSDate date];
[self setValue:date forKey:@"id"];
}
We will use the current date stamp as our unique identifier. If you need to support inserting fields, then you will need to come up with another type of unique identifier.
Next, you will need to setup the Sort Descriptor binding for your NSArrayController class. Here is the function I used in the AppDelegate.m file:
- (NSArray*) idSortDescriptors
{
NSSortDescriptor *sorter = [[[NSSortDescriptor alloc] initWithKey:@"id" ascending: YES] autorelease];
return ([NSArray arrayWithObject:sorter]);
}
This works for me; however, your mileage my vary.
vBulletin® v3.8.4, Copyright ©2000-2010, Jelsoft Enterprises Ltd.