birdcage
birdcage

Reputation: 2676

CoreData saving object is not successful

I am trying to update int data by using CoreData but I think I am missing some parts before updating the object. Here what I am trying to do to save the int value. Here I want to both save and update only first object value.

- (IBAction)btnYakClicked:(UIButton *)sender
{
    NSManagedObjectContext *context = [self managedObjectContext];

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

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

    NSManagedObject* kaloriObject = [results objectAtIndex:0];

    NSNumber *valWeek = [NSNumber numberWithInteger:300];
    NSNumber *valMonth = [NSNumber numberWithInteger:1000];

    [kaloriObject setValue:valWeek forKey:@"thisweek"];
    [kaloriObject setValue:valWeek forKey:@"thismonth"];
}

Upvotes: 0

Views: 37

Answers (1)

sooper
sooper

Reputation: 6039

I don't see the line where you're saving it. For example:

NSError *saveError;

if (![context save:&saveError]) {
    NSLog(@"Error: %@ %@", saveError, [saveError localizedDescription]);
}

It might also be a good idea to limit the fetch request since you're only looking for the first object:

[request setFetchLimit:1];

I would hope that this would improve performance but cannot confirm.

Upvotes: 1

Related Questions