Search code examples
ioscore-datansmanagedobjectcontext

Core Data Entity is not updating


I am trying to update a boolean stored within a core data entity and it just does not seem to update no matter what I try. Here is the method I am using:

+ (void) updateTimeFormatPreferenceForUserWithNumber:(NSString *)number withBool:(BOOL)preference WithContext:(NSManagedObjectContext *)context {

    NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"mobileNumber == %@", number];

    [fetchRequest setPredicate:predicate];
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"User" inManagedObjectContext:context]];
    [fetchRequest setIncludesPropertyValues:YES];

    NSError *error = nil;
    NSArray *result = [context executeFetchRequest:fetchRequest error:&error];

    if ([result count] > 0){

        User *user = [result objectAtIndex:0];
        [user setTwentyFourHourTime:[NSNumber numberWithBool:preference]];

        if (![context save:&error]) {
            NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
        } else {

            BOOL twentyFourHour = [[CoreDataUtils getUserWithNumber:number WithContext:context] twentyFourHourTime];

            NSLog(@"Saved Preference %s", preference ? "True" : "False");
            NSLog(@"From user object: %@", [user twentyFourHourTime]);
            NSLog(@"Twenty Four Hour Time is: %s", twentyFourHour ? "true" : "false");
        }
    }
}

Calling this method with the preference property as both true and false has the following output:

Saved Preference False
From user object: 0
Twenty Four Hour Time is: true
Saved Preference True
From user object: 1
Twenty Four Hour Time is: true
Saved Preference False
From user object: 0
Twenty Four Hour Time is: true
Saved Preference True
From user object: 1
Twenty Four Hour Time is: true

The twentyForHour boolean never changes and its driving me crazy, why does it not update?

The getUserWithNumber method is:

+ (User *) getUserWithNumber:(NSString *)number WithContext:(NSManagedObjectContext *)context {

    NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"mobileNumber == %@", number];

    [fetchRequest setPredicate:predicate];
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"User" inManagedObjectContext:context]];
    [fetchRequest setIncludesPropertyValues:YES];

    NSError *error = nil;
    NSArray *result = [context executeFetchRequest:fetchRequest error:&error];

    if ([result count] > 0){
        return [result objectAtIndex:0];
    } else {
        return nil;
    }
}

Thankyou


Solution

  • This line

    BOOL twentyFourHour = [[CoreDataUtils getUserWithNumber:number WithContext:context] twentyFourHourTime];
    

    is incorrect because you're saving an NSNumber instance into a BOOL variable, so you're really testing if the number has ever been set rather than the value of the bool. Change it to

    BOOL twentyFourHour = [[[CoreDataUtils getUserWithNumber:number WithContext:context] twentyFourHourTime] boolValue];