Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreData Edit/Overwrite Object

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.

like image 382
Josh Kahane Avatar asked Nov 27 '10 17:11

Josh Kahane


People also ask

How do you update an object in core data?

To update a specific object you need to set up a NSFetchRequest . This class is equivalent to a SELECT statetement in SQL language. The array results contains all the managed objects contained within the sqlite file. If you want to grab a specific object (or more objects) you need to use a predicate with that request.

What is NSManagedObject in core data?

In some respects, an NSManagedObject acts like a dictionary—it's a generic container object that provides efficient storage for the properties defined by its associated NSEntityDescription instance.

How can you retrieve data from core data?

Fetching Data From CoreData We have created a function fetch() whose return type is array of College(Entity). For fetching the data we just write context. fetch and pass fetchRequest that will generate an exception so we handle it by writing try catch, so we fetched our all the data from CoreData.

How does Core Data Work?

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.


2 Answers

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.

like image 144
Matthew Frederick Avatar answered Oct 23 '22 02:10

Matthew Frederick


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!

like image 4
MCannon Avatar answered Oct 23 '22 03:10

MCannon