Fxbaez
Fxbaez

Reputation: 193

update an attribute using core data

I have an entity that has 3 attributes: firstname, lastname and score. i am able to search the score of a person by firstname and last name and display it on an UILabel (so far so good) my problem is that i don't know how to update the score attribute with a new value. below the code that i use to perform the search and display the old value.

NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"Scorecard" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entitydesc];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstname like %@ and lastname like %@", self.firstnameTextField.text, self.lastnameTextField.text];
[request setPredicate:predicate];

NSError *error;
NSArray *integer = [context executeFetchRequest:request error:&error];

if(integer.count <= 0){
    self.displayLabel.text = @"No records found";

}

else {

    NSNumber *score;

    for (NSManagedObject *object in integer) {


       score = [object valueForKey:@"score"];

        int x = [score intValue];

       // New Value is going to be the old value + 2
        int y = x + 2;

      //displays old value found
        [self.displayLabel setText:[NSString stringWithFormat:@"old value %d",x]];

    }

}

update #1

i was able to update the attribute and save it however i had to convert my new Integer value to a string and then convert it back to an integer. i don't like the extra step. if anyone has another suggestion please let me know and thanks for the help. below the updated code.

 NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"Scorecard" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entitydesc];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstname like %@ and lastname like %@", self.firstnameTextField.text, self.lastnameTextField.text];
[request setPredicate:predicate];

NSError *error;
NSArray *integer = [context executeFetchRequest:request error:&error];

if(integer.count <= 0){
    self.displayLabel.text = @"No records found";

}

else {

    NSNumber *score;

    for (NSManagedObject *object in integer) {

        score = [object valueForKey:@"score"];

        int x = [score intValue];
        int y = x + 2;
        NSString* z = [NSString stringWithFormat:@"%d",y];

        [object setValue:[NSNumber numberWithInteger:[z integerValue]] forKey:@"score"];
        [self.displayLabel setText:[NSString stringWithFormat:@"Value updated to %d",y]];
        [context save:&error];

    }

}

update 2

Thanks to Kevin's answer I was able to solve my problem. I am now able to replace an integer value from an attribute on my core data with another integer value. below my code finally working.

NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"Scorecard" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entitydesc];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstname like %@ and lastname like %@", self.firstnameTextField.text, self.lastnameTextField.text];
[request setPredicate:predicate];

NSError *error;
NSArray *integer = [context executeFetchRequest:request error:&error];

if(integer.count <= 0){
self.displayLabel.text = @"No records found";

}

else {

NSNumber *score;

for (NSManagedObject *object in integer) {

    score = [object valueForKey:@"score"];

    int x = [score intValue];
    int y = x + 2;

    [object setValue:[NSNumber numberWithInt:y] forKey:@"score"];
    [self.displayLabel setText:[NSString stringWithFormat:@"Value updated to %d",y]];
    [context save:&error];

}

}

Upvotes: 1

Views: 680

Answers (2)

Kevin
Kevin

Reputation: 17556

Get an object like you did before then set's it's score property

[object setValue:[NSNumber numberWithInt:y] forKey:@"score"]; 

Upvotes: 1

J2theC
J2theC

Reputation: 4452

My first suggestion would be to use NSManagedObject's subclasses instead of using primitives. You can do this by using the core data inspector and changing your entity class.

Now, to do a change to the score, you need to find the object you need to change, and assign the value. Once the value is assigned, you will need to save the NSManagedObject the object belongs to. Once the context successfully saves, your changes are persistent.

Upvotes: 0

Related Questions