Re: Better Way to Add an Entity to an ArrayController
Re: Better Way to Add an Entity to an ArrayController
- Subject: Re: Better Way to Add an Entity to an ArrayController
- From: vincent habchi <email@hidden>
- Date: Sat, 23 Jan 2010 23:02:40 +0100
Le 23 janv. 2010 à 21:43, cocoa-dev a écrit :
> I have a core data app with a model where there is a one-to-many relationship between CD and Track (CD has an NSSet called tracks). The tracks are shown in an NSTableView and the content set is managed by an NSArrayController. To manually add a track the user presses the add button which runs the "add:" selector of the arrayController. Right now, to programmatically add a track I do:
>
> [arrayController performSelector:@selector(add:) withObject:nil afterDelay:0.0];
>
Why just do not use [arrayController insertObject:newTrack atOffset:0]?
> Track *newTrack = [[Track alloc] init];
If Track is somehow a subclass of NSManagedObject, then it's not the right way to create it. You should use:
NSEntityDescription * newTrackDesc = [NSEntityDescription entityForName:@"Track" inManagedObjectContext:moc];
Track * newTrack = [[Track alloc] initWithEntity:newTrackDesc insertIntoManagedObjectContext:moc];
where "moc" is a pointer to your managed object context.
V.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden