1

I've got an UITableView with Core Data.

When I insert new records or update an existing record everything goes well and the UITableView updates automatically, but when I delete a record the UITableView freezes until the UIView is reloaded.

Does anybody know what is going wrong?

- (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
{
    UITableView *tableView = self.tableView;

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

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

The code where I delete a record and saves the context

- (void)refreshData
{
    dispatch_queue_t fetchQ = dispatch_queue_create("Data Fetch", NULL);
    dispatch_async(fetchQ, ^{

        // here I delete some records            

        dispatch_async(dispatch_get_main_queue(), ^(void) {
            [self saveContext];
        });
    });
}
- (void)saveContext
{
    NSError *error = nil;
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil) {
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
             // Replace this implementation with code to handle the error appropriately.
             // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        } 
    }
}
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
robbert0
  • 11
  • 4
  • Have you tried self.tableview end updates in the case where you remove the row. That might end up the animation. Just a thought – Adrian P May 16 '13 at 14:59
  • Sounds as if your begin- and endUpdates might not be called pairwise. Have you tried to log each call to them next to statement and then see in the log if they occur in the right order and in pairs? – NSSplendid May 16 '13 at 15:13
  • @NSSplendid When I set some breakpoints first it hits controllerWillChangeContent. Then it hits deleteRowsAtIndexPaths. End in the end it hits controllerDidChangeContent. It looks like the right order. – robbert0 May 16 '13 at 18:35
  • Could you show how do you delete rows? Why do you use a dispatch queue? – Lorenzo B May 16 '13 at 20:06

1 Answers1

0

When I remove the dispatch queue everything works fine.

robbert0
  • 11
  • 4