James
James

Reputation: 55

Programmatically update duplicate values in coredata?

I am new in Core Data. I want to update duplicate values. For example my table looks like this

 id | Name
============
 1  | Joseph  
 2  | Fernandez  
 3  | Joseph
 4  | James

Say that I want to update Joseph corresponding to id 1 and 4 to "myName". When I tried to update this it only updates the 4th row. I can't find any way to do this in any of the documentation. Can anyone suggest me a solution?

One more question, how can I print all name values?

Upvotes: 3

Views: 493

Answers (3)

nidhu
nidhu

Reputation: 31

you will have to read over the documentation to know how to update record http://www.appcoda.com/core-data-tutorial-update-delete/

Upvotes: 2

ggfela
ggfela

Reputation: 1152

It's as simple as retrieving the NSManagedObject and changing the Name property. You can retrieve the NSManagedObject with a fetch request. Once you changed the property and you want to keep it changed even when you close the application you'll have to do a save on the managedObjectContext.

You'll have to read over the documentation to get up to speed on core data: http://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/CoreData/Articles/cdBasics.html#//apple_ref/doc/uid/TP40001650-TP1

Edit: just NSLog whatever you want to know, for example log you fetch request results.

Upvotes: 0

Lorenzo B
Lorenzo B

Reputation: 33428

James,

I'll try to reply to both your questions with sample code.

To update specific objects you need to se up a new NSFetchRequest with a predicate, grab the objects (of type NSManagedObject), update the values you are interested in and save the context.

So, for example:

NSFetchRequest* fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"YourEntityName"];
// set the predicate (it's equal to set a WHERE SQL clause) filtering on the name for example
// use camel case notation if possible, so instead of Name use name (for this you have to changes your model, if you don't want to do it use Name)
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"name == %@", @"Joseph"]];

NSError* error = nil;
NSArray* results = [context executeFetchRequest:fetchRequest error:&error];
// do some error checking here...
for (NSManagedObject resultItem in results) {

    // use KVC (for example) to access your object properties
    [resultItem setValue:@"myName" forKey:@"name"];
}

// save your context here
// if you don't save, changes are not stored

To print you need to se up a new NSFetchRequest, grab the objects (of type NSManagedObject) and use NSLog.

So for example:

NSFetchRequest* fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"YourEntityName"];

NSError* error = nil;
NSArray* results = [context executeFetchRequest:fetchRequest error:&error];
// do some error checking here...
for (NSManagedObject resultItem in results) {

    NSLog(@"%@", [resultItem valueForKey:@"name"]);
}

P.S. The code I provided is quite simple and the predicate I used to specific values check against the name. Since this could be error prone, I would modify the model and using a sort of guid for each objects you need to use (I don't know if id is for that but I would change its name to another one, for example userId). Once done you can check against it.

Hope that helps.

Upvotes: 1

Related Questions