Saving Changes to Managed Objects

Discussion in 'iOS Programming' started by xcodeNewbie, Oct 3, 2011.

  1. xcodeNewbie macrumors member

    Jul 1, 2011
    I'm trying to edit a property on a managed object, but for some reason the changes aren't being saved. I'm using the following code:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        if (indexPath.section == 3) {
            NSManagedObjectContext *context = [[self appDelegate] managedObjectContext];
            NSNumber *num = [self.word valueForKey:@"active"]; // "word" is a managed object
            if ([num isEqualToNumber:[NSNumber numberWithInt:1]]) {
                num = [NSNumber numberWithInt:0];
            } else {
                num = [NSNumber numberWithInt:1];
            [self.delegate wordWillBecomeInactive:self.word]; // Just a little protocol I wrote
            [self.word setValue:num forKey:@"active"];
            NSLog(@"%i", [context hasChanges]); // Prints 0
            NSLog(@"%@", [self.word changedValues]); // Prints an empty dictionary
            NSError *error = nil;
            [context save:&error];
            [self.tableView reloadData];
    If I NSLog the value "active" then it shows up as being toggled, but it doesn't save. Anybody know what's happening?
  2. admanimal macrumors 68040

    Apr 22, 2005
    Did you check the value of error after calling save on the context?
  3. xcodeNewbie thread starter macrumors member

    Jul 1, 2011

    I NSLoged the error, and it was (null).

Share This Page