1
我有一个UITableView
核心数据。deleteRowsAtIndexPaths冻结UITableView
当我插入新记录或更新现有记录时,一切进展顺利,UITableView
会自动更新,但是当我删除记录时,UITableView
冻结,直到UIView
重新加载。
有人知道发生了什么问题吗?
- (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];
}
,我删除记录并保存上下文
- (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();
}
}
}
您是否在尝试过self.tableview结束更新的情况下删除该行。这可能会结束动画。只是一个想法 –
听起来好像你的begin和endUpdates可能不会被称为pairwise。您是否尝试将每个呼叫记录到语句旁边,然后在日志中查看它们是否以正确的顺序和成对出现? – NSSplendid
@NSSplendid当我首先设置了一些断点时,它碰到了controllerWillChangeContent。然后它击中deleteRowsAtIndexPaths。最后,它会触及controllerDidChangeContent。它看起来像正确的顺序。 – robbert0