Tanner
Tanner

Reputation: 823

Why does my boolean value stored in Core Data not show up immediately?

Ive been working on a list app where core data is used to persist a checkmark Everything was fine till xcode crashed. No code was changed but now instead of a checkmark showing up immediatly you have to click the row, quit the app, then relaunch it to get anything. This is the code along with an other question of how I got the code in the first place. How can a checkmark state be saved in core data?

Any help is greatly appreciated. Thanks

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSManagedObject *selectedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];


    if ([[selectedObject valueForKey:@"check"] boolValue]) {
        [selectedObject setValue:[NSNumber numberWithBool:NO] forKey:@"check"];
    } else {
        [selectedObject setValue:[NSNumber numberWithBool:YES] forKey:@"check"];
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *defaultCellIdentifier = @"Item";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:defaultCellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:defaultCellIdentifier] autorelease];
    }

    NSManagedObject *item = [[self fetchedResultsController] objectAtIndexPath:indexPath];
    cell.textLabel.text = [item valueForKey:@"name"];

    if ([[item valueForKey:@"check"] boolValue]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}



-(UITableViewCellAccessoryType)tableView:(UITableView *)tableView accesoryTypeForRowWithInddexPath:(NSIndexPath *)indexPath {
    return UITableViewCellAccessoryNone;
}

Upvotes: 1

Views: 975

Answers (2)

Brad Larson
Brad Larson

Reputation: 170319

Have you implemented the necessary NSFetchedResultsControllerDelegate methods to catch updates to your data model? If not, your table won't know to update the row's visual state. Some boilerplate code for this is as follows:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller 
{
    [self.tableView beginUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type 
{   
    switch(type) 
    {
        case NSFetchedResultsChangeInsert:
        {
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
        }; break;

        case NSFetchedResultsChangeDelete:
        {
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
        }; break;
    }
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath 
{   
    switch(type) 
    {
        case NSFetchedResultsChangeInsert:
        {
            // This works around problems with inserting a row at the beginning of the list
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:(newIndexPath.row + 1) inSection:newIndexPath.section]] withRowAnimation:UITableViewRowAnimationFade];
        }; break;

        case NSFetchedResultsChangeDelete:
        {
            if (self.tableView.editing)
                [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:(indexPath.row + 1) inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationFade];
            else
                [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }; break;

        case NSFetchedResultsChangeUpdate:
        {
            [self updateCell:[self.tableView cellForRowAtIndexPath:indexPath] fromCategory:[fetchedResultsController objectAtIndexPath:indexPath]];
        }; break;

        case NSFetchedResultsChangeMove:
        {
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
        }; break;
    }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{
    [self.tableView endUpdates];
}

Upvotes: 2

RickiG
RickiG

Reputation: 11390

Does your core data model have a "default value"? Maybe it has been set to "NO" instead of "YES".

Upvotes: 0

Related Questions