【问题标题】:Core Data: Do we need to re-fetch after every delete?核心数据:每次删除后我们需要重新获取吗?
【发布时间】:2014-08-16 20:23:47
【问题描述】:

您好,有人可以帮帮我吗?

我将 核心数据tableview 控制器 一起使用。我正在使用 NSFetchedResultsController (NSFRC)NSManagedObjectContext (NSMOC)

1) 我在视图内做NSFRC.performFetch 确实加载了 2)当用户删除我正在做的一行时NSMOC.deleteObject

应用崩溃,抱怨删除后行数没有减少

在删除解决它之后执行NSFRC.performFetch。但我认为它应该可以在我们不必强制获取的情况下工作?请问有人可以向我解释一下吗?顺便说一下,我正在使用 IOS 8 (beta 5) 和 Swift

【问题讨论】:

  • 您能否向我们展示您对tableView:commitEditingStyle:forRowAtIndexPath: 和各种 NSFetchedResultsController 委托方法的实现?
  • 我的解决方案对您有用吗?除了我发布的代码之外,您还必须在删除时调用委托方法之前声明您的视图控制器实现 NSFetchedResultsControllerDelegate。
  • CoffeCoder,请将答案标记为正确。当有人帮助您时,请谨慎行事。

标签: ios core-data swift ios8 nsfetchedresultscontroller


【解决方案1】:

没有。您可以实现 NSFetchedResultsControllerDelegate 方法来捕获更改,以便在下划线数据更改(例如,删除和添加)时通知 NSFetchedResultsController。

- (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:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

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


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

【讨论】:

  • 另外,声明你的视图控制器实现了 NSFetchedResultsControllerDelegate。
猜你喜欢
  • 1970-01-01
  • 2019-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-25
相关资源
最近更新 更多