Search code examples
iphoneioscore-datansfetchrequest

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

}

}

Solution

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

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