There are two ways to insert data into Core Data - and whichever one you use is up to you. However, one of them depends on whether you have generated Model classes for your data model for the Core Data db.
The regular way is to use the following:
NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:@"table"
inManagedObjectContext:context];
[object setValue:@"value1" forKey:@"stringColumn"];
[object setValue:12 forKey:@"numberValue"];
NSError *error;
if (![context save:&error]) {
NSLog(@"Failed to save - error: %@", [error localizedDescription]);
}
This is assuming you've already got your managed object context set up.
It is much more efficient if you create and insert your objects into the context in a loop, and then save after the loop ends.
The other method isn't much different, but is much safer in terms of type safety. If you have generated model classes (which you can do from the
xcdatamodels) then you can simply create an object of that class and set its properties.
TableObject *object = [NSEntityDescription insertNewObjectForEntityForName:@"table"
inManagedObjectContext:context];
[object setStringColumn:@"value1"];
[object setNumberValue:12];
NSError *error;
if (![context save:&error]) {
NSLog(@"Failed to save - error: %@", [error localizedDescription]);
}
To delete from a table, simply retrieve the object from the table (I'm assuming you are using the second method here for insertions, and as such have generated model classes) and use the following:
[context deleteObject:object];
Note that you will need to call save for that to take effect as well.
Hope this helps! Good luck!
EDIT:
Sorry, I must've misread the question!
To examine an existing record, you'll want to create a Fetch Request, and then execute it on your managed object context.
At bare minimum, a Fetch Request
answered 2010-11-05T00:09:15.793