【问题标题】:Deleting a CoreData object, then calling [tableView reloadData] causes a SIGABRT删除 CoreData 对象,然后调用 [tableView reloadData] 会导致 SIGABRT
【发布时间】:2010-08-22 17:56:45
【问题描述】:

我支持删除包含 tableView 的 UIView 中的行:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
  [self.fetchedResultsController.managedObjectContext
            deleteObject:[self.fetchedResultsController
            objectAtIndexPath:indexPath]];
  NSError *error;
  if (![[self.fetchedResultsController managedObjectContext] save:&error]) {
   NSLog(@"Unresolved error %@, %@, %@", error, [error userInfo],
                    [error localizedDescription]);
  }
  else {NSLog(@"Apparently successful");}
  [self.tableView reloadData];
 }
}

当我尝试此操作时,我在控制台中收到“显然成功”消息,但随后在 configureCell 中收到 SIGABRT。就好像某处对象没有被删除,而 configureCell 正试图呈现一个丢失的对象。 (但是,当我重新运行该应用程序时,记录就消失了。)

有什么想法吗?

【问题讨论】:

  • 发布更多代码会对我们有所帮助,尤其是您的 tableView:cellForRowAtIndexPath: 方法,这可能是这里的罪魁祸首。
  • 好的,这是崩溃前发生的事情: - (UITableViewCell )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { [...不适合的样板文件此评论...] [self configureCell:cell atIndexPath:indexPath];返回单元格; } - (void)configureCell:(UITableViewCell)cell atIndexPath:(NSIndexPath*)indexPath { NSManagedObject *mo = [fetchedResultsController objectAtIndexPath:indexPath]; int objtype = [[(Type *)[mo valueForKey:@"type"] globalid] intValue];然后——砰!

标签: ios iphone uitableview core-data


【解决方案1】:

在使用 NSFetchedResultsController 时,您不应该在此处使用 [self.tableView reloadData]

当您删除一个对象时,将为您调用正确的NSFetchedResultsControllerDelegate 方法,您应该在该方法中更新您的表格视图。

创建一个空的基于导航的应用程序并选中使用核心数据进行存储复选框。您将得到一个空项目,它可以正确执行所有这些操作。这将是一个很好的例子。

【讨论】:

  • 嗯——不确定。我删除了 [self.tableView reloadData] 并且表格没有更新(行没有消失)。当我关闭 ViewController(一个模态 VC)并重新打开它时,我得到了相同的 SIGABRT。这是一个包含 UITableView 作为属性的 ViewController,实现了
  • 然后显示更多代码。 SIGABRT 还带有一个堆栈跟踪,可能会提供一个很好的提示。
  • 如果表没有更新,那么您没有正确实现NSFetchedResultsController 的委托方法。 @St3fan 的回答是正确的。显示完整的错误也会很有帮助。
  • 我终于有机会更仔细地研究一下,你说得对,我没有正确实现 NSFetchedResultsControllerDelegate —— Apple 文档对我有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
  • 2011-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多