Re: Creating a managed object without adding it to the context?
Re: Creating a managed object without adding it to the context?
- Subject: Re: Creating a managed object without adding it to the context?
- From: Andreas Mayer <email@hidden>
- Date: Sun, 25 Jan 2009 08:39:30 +0100
Am 24.01.2009 um 23:51 Uhr schrieb Jerry Krinock:
On 2009 Jan 24, at 14:13, Ulai Beekam wrote:
How do I programmatically create a managed object (with respect to
some entity in a particular context of course) but WITHOUT adding
it to the context?
I do not believe this is possible. A managed object without a
context would not be "managed".
It is possible. Just pass nil as context:
NSEntityDescription *entityDescription = [[theManagedObjectModel
entitiesByName] objectForKey:@"EntityName"];
NSManagedObject *newObject = [[NSManagedObject alloc]
initWithEntity:entityDescription insertIntoManagedObjectContext:nil];
The reason I want this is to be able to take that new managed
object and add it via an NSArrayController (one that is connected
to said context of course) instead. That way I will get all the
benefits of the array controller, such as the newly inserted object
getting selected in a table view that is bound to the array
controller.
You indicated in your first sentence that you know which managed
object context the new object is destined for. I don't see the
problem. Just insert it when you create it.
I am using the method shown above when I don't want to insert an
unfinished object. I.e. I have some sort of input form and want to
bind the fields to my model's properties. But I don't want the new
object to show up in any list view before data input is finished.
So I create a new object that is not yet part of the managed object
context and insert it after editing is finished.
(You also need some place to store that object that you can bind to
while editing; like a property of a window controller or such.)
Andreas
_______________________________________________
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