I have this code sample demonstrating how to update an object in core data, however I am having a little i开发者_Python百科ssue:
// Retrieve the context
if (managedObjectContext == nil) {
managedObjectContext = [(YourAppNameAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
// Retrieve the entity from the local store -- much like a table in a database
NSEntityDescription *entity = [NSEntityDescription entityForName:@"YourEntityName" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
// Set the predicate -- much like a WHERE statement in a SQL database
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"YourIdentifyingObjectProperty == %@", yourIdentifyingQualifier];
[request setPredicate:predicate];
// Set the sorting -- mandatory, even if you're fetching a single record/object
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"yourIdentifyingQualifier" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release]; sortDescriptors = nil;
[sortDescriptor release]; sortDescriptor = nil;
// Request the data -- NOTE, this assumes only one match, that
// yourIdentifyingQualifier is unique. It just grabs the first object in the array.
YourEntityName *thisYourEntityName = [[managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:0];
[request release];
request = nil;
This line:
YourEntityName *thisYourEntityName = [[managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:0];
Im not understanding what I should be using in place of 'YourEntityName', from what I gathered form who was helping me is that I must use the entity name form the data model, but it doesn't appear to work, I merely get an undeclared error.
I have an entity named 'Event' and that entity has two attributes named userNote and timeStamp.
Im working with this with an essentially brand new clean split view ipad project using core data. I want to run this in a textViewDidEndEditing so when the user has finished typing their note, its updates the object.
Replace YourEntityName
with the name of the class used to represent your entity. If you have declared a custom class for your entity in Xcode, specify that class here. In your case, it sounds like you haven't declared a custom class for your entity. In that case, use NSManagedObject
as the entity class.
In Xcode's data model editor, you can specify both a name and a class for an entity. They are not the same thing. The entity name is used to refer to the entity in statements like this:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
The entity class specifies the class to use for that entity's managed objects. When using Core Data, developers often create custom classes to use for their entities, but doing so is not required. If you do want to use a custom class for an entity, you must create that class yourself (as a subclass of NSManagedObject
) and specify that class name in the data model editor in Xcode. If you don't specify a custom class, NSManagedObject
is used to represent entity objects.
精彩评论