CoreData Edit/Overwrite Object

18,314

Solution 1

You simply request the existing object using an NSFetchRequest, change whatever fields need to be updated (a simple myObject.propertyName setter is all that's required), and then perform a save action on the data context.

EDIT to add code example. I agree with MCannon, Core Data is definitely worth reading up about.

This code assumes you created the project with a template that includes Core Data stuff, such that your app delegate has a managed object context, etc. Note that there is NO error checking here, this is just basic code.

Fetching the object

// 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;

Update the object

thisYourEntityName.ExampleNSStringAttributeName = @"The new value";
thisYourEntityName.ExampleNSDateAttributeName = [NSDate date];

Save the change

NSError *error;
[self.managedObjectContext save:&error];

Now your object/row is updated.

Solution 2

http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/Articles/cdFetching.html will show you how to fetch an entity,

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdUsingMOs.html will show you how to change properties, and save them.

core data is something where you really want to read a lot of the apple documentation and become familiar, it will save you hours in the long run. hope this helps!

Share:
18,314

Related videos on Youtube

Josh Kahane
Author by

Josh Kahane

Updated on May 10, 2022

Comments

  • Josh Kahane
    Josh Kahane over 1 year

    I am playing around with a new project, a split view iPad app using Core Data, and I was wondering, as its fairly clear how to add and remove an item. If I was to say alter this to hold text, then that text be shown in a UITextView, how can I edit or overwrite an object in CoreData?

    So the user types their note in the UITextView and when they leave that it edits and saves the note (object in the table view) they have currently selected.

    Appreciate any help thanks.

  • Josh Kahane
    Josh Kahane about 13 years
    Ok, could you point me to or make a little code sample please? Sorry, Im fairly new to CoreData and having a sample would really help me in understanding.
  • Josh Kahane
    Josh Kahane about 13 years
    Im certainly reading up, thanks for the help. However a couple of things: The fetch request, does it replace the pre made - '(NSFetchedResultsController *)fetchedResultsController'? Also the 'YourEntityName *thisYourEntityName' is confusing me, the actual entity name? I just get errors. And the update and save chunks, they stay with the fetch code right?
  • Matthew Frederick
    Matthew Frederick about 13 years
    Everywhere I wrote "your something" you'll replace with your actual names. Substitute the name of your entity for YourEntityName. In your example it might be Note, or whatever you called it in your data model. thisYourEntityName is simply a single record/object; it just needs a name. If your entity was Note then you might use thisNote, but it's just a variable, call it whatever you like. As to staying together, sure, this all runs together in sequence. You might create a class with a method called NoteUpdater or such, a method that receives the note identifier and what the new text should be.
  • Josh Kahane
    Josh Kahane about 13 years
    Ahh ok, I have given that a shot. Still puzzled with an undeclared error for that line 'YourEntityName *thisYourEntityName'. My entity is called 'Event' and it has two attributes called 'userNote' and 'timeStamp'.
  • Josh Kahane
    Josh Kahane about 13 years
    So I have: Event *myEntity = [[managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:0]; But 'Event' is undeclared. Im not quite understanding.
  • Matthew Frederick
    Matthew Frederick about 13 years
    It's likely you've neglected to include your Event.h
  • Sabapathy
    Sabapathy almost 7 years
    Page Not Found.