Re: CoreData: Accessing specific ManagedObjects
Re: CoreData: Accessing specific ManagedObjects
- Subject: Re: CoreData: Accessing specific ManagedObjects
- From: Joshua Scott Emmons <email@hidden>
- Date: Tue, 28 Mar 2006 15:53:30 -0600
What I want, is when I hit a button, to be able to change a value
that is displayed
and for these change to be saved in the CoreData database.
The problem with the above code, is that when it calls the
"initWithEntity: insertIntoManagedObjectContext" method it creates a
new instance of my entity, instead of modifying the one that is
currently displayed on the screen.
Right. That's what initWithEntity:insertIntoManagedObjectContext:
does. The code you list creates a brand new Girl instance and inserts
this new instance into the managed object context. If your
application already had a Girl instance on startup, after clicking
this button you will have two.
If what you want to do is change the values of an existing instance,
you DO NOT want to create and insert a new one! What you want to do
is get a hold of the existing one so that you may modify its values
via KVC.
There are two basic ways to do this, depending on the structure of
your app.
First, if your app creates a single instance of your entity object
("Girl", in this case) and uses that throughout its life, you can
simply fetch all managed objects that match that entity:
NSManagedObjectContext *moc = [self context];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSArray *fetchResults;
@try {
[fetchRequest setEntity:[NSEntityDescription
entityForName:@"Girl" inManagedObjectContext:moc]];
fetchResults = [moc executeFetchRequest:fetchRequest error:nil];
} @finally {
[fetchRequest release];
}
//assume one, and only one, thing was returned...
Girl *girl = [fetchResults objectAtIndex:0];
[girl setValue:@"hello world" forKey:@"myProperty"];
If there is only one Girl instance in your app, this will return it
and you can go to town all the -setValue:ForKey:s you want. If you
interfaces are all bound to properties in this instance, they will
automagically be updated via KVO to the new values you set with -
setValue:forKey:
The other (more likely?) possibility is that you have an
NSArrayController in your nib that is creating multiple instances of
your Girl subclass every time an "Add" button is pressed. Your
interface controls are probably bound to NSArrayController's
"selection" key, which points to the currently selected Girl instance.
In this case you cannot just fetch all Girl instances, because you'll
get all the unselected ones along with the selected one. And you
won't be able to distinguish them, because the only thing that keeps
track of what instance is "selected" is the NSArrayController. Thus,
to get to the selected instance, you're going to need to create an
outlet to the NSArrayController in your nib. After that's done, it's
easy to change a property of the selected instance. You can do
something like like:
[myArrayControllerOutlet setValue:@"hello world"
forKeyPath:@"selected.myProperty"];
Once again, anything bound to NSArrayController's selected key will
automatically update its content to match whatever you change the
property to because it's been notified of the change via KVO.
Cheers,
-Joshua Emmons
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden