CoreData: Accessing specific ManagedObjects
CoreData: Accessing specific ManagedObjects
- Subject: CoreData: Accessing specific ManagedObjects
- From: "Bobby B" <email@hidden>
- Date: Tue, 28 Mar 2006 17:21:42 -0400
Hello everyone;
I am using the following code:
- (IBAction)momAction:(id)sender
{
NSManagedObjectModel * mom = [self managedObjectModel];
NSManagedObjectContext * moc = [self managedObjectContext];
NSEntityDescription * runEntity = [[mom entitiesByName] objectForKey:@"Girl"];
Girl * girl = [[[Girl alloc] initWithEntity:runEntity
insertIntoManagedObjectContext:moc] autorelease];
[girl setValue:@"HELLO WORLD" forKey:@"name"];
NSError * error = nil;
if (![moc save: &error]) {
NSLog(@"Error while saving\n%@", ([error localizedDescription] !=
nil) ? [error localizedDescription] : @"Unknown Error");
exit(1);
}
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:runEntity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"name" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
error = nil;
NSArray *array = [moc executeFetchRequest:request error:&error];
if ((error != nil) || (array == nil)) {
NSLog(@"Error while fetching\n%@", ([error localizedDescription] !=
nil) ? [error localizedDescription] : @"Unknown Error");
exit(1);
}
NSEnumerator *runEnumerator = [array objectEnumerator];
while (girl = [runEnumerator nextObject]) {
NSLog(@"name: %@ \n phone1: %@",
[girl valueForKey:@"name"],
[girl valueForKey:@"phone1"]);
}
}
Girl is a subclass of NSManagedObject. It is my "entity" for my
Coredata object model.
I have been struggling with CoreData for a while now, trying to
accomplish a specific thing. When I run my program, everything from
the database is displayed properly, via bindings. 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.
I've read the low level CoreData tutorial, the high level one, the
Persistance Document one, etc. That's how I've gotten all the above
code. 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.
The code towards the bottom just lists all records in the database.
I feel like I've read every document I can find on CoreData, but I
can't find out how to modify the specific entry that is selected. The
closest I can think to do is to use the enumerator above, and look for
the record that matches the one I need (and I'd have to match it to
the nameOutlet.stringValue, etc.
May someone suggest what tree I should be barking up?
Thank you
BB
_______________________________________________
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