【问题标题】:NFetchedResultsController delegate method didChangeContentWith is not called when deleted an item using UIDIffableDataSource tableview使用 UIDIffableDataSource tableview 删除项目时,不调用 NFetchedResultsController 委托方法 didChangeContentWith
【发布时间】:2020-03-19 22:56:14
【问题描述】:

我正在尝试使用 UITableViewDiffableDataSource 实现我现有的 coredata 项目。我的 tableview 是使用 NSFetchedResultsController 和相应的委托方法耦合的。 我可以使用 diffabledatasource 列出 tableview 中的数据。我的数据源使用以下泛型类型声明

UITableViewDiffableDataSource<String, NSManagedObjectID>

为了在 tableview 中启用编辑模式,我对 UITableViewDiffableDataSource 进行了子类化。我可以从表格视图中删除单元格,但不能从我的 coreData 中删除。删除单元格的代码如下

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
  if editingStyle == .delete {
     if let identifierToDelete = itemIdentifier(for: indexPath){
        var snapshot = self.snapshot()
        snapshot.deleteItems([identifierToDelete])
        apply(snapshot)
     }
  }}

当我删除单元格时,没有调用下面的 NSFetchedResultsControllerDelegate 方法。

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeContentWith snapshot: NSDiffableDataSourceSnapshotReference) 

我不确定这是否是将 diffabledatasource 与 NSFetchedResultscontroller 耦合的正确方法。任何帮助将不胜感激。提前致谢

【问题讨论】:

    标签: ios swift nsfetchedresultscontroller diffabledatasource nsdiffabledatasourcesnapshot


    【解决方案1】:

    而不是在UITableViewDiffableDataSource 实现中覆盖tableView(_:commit:)

    override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { ...
    

    在视图控制器中并通过索引路径从获取的结果控制器中获取对象。

    【讨论】:

      【解决方案2】:

      您不应该编辑快照,而是编辑模型。 IE。删除托管对象如下:

      - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {  
          if (editingStyle == UITableViewCellEditingStyleDelete) {  
              NSManagedObjectContext *context = self.managedObjectContext;  
              NSManagedObjectID *objectID = [self itemIdentifierForIndexPath:indexPath];  
              Event *event = [context objectWithID:objectID];  
              [context deleteObject:event];  
              NSError *error = nil;  
              if (![context 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();  
              }  
          }  
      }  
      

      删除对象后,didChangeContentWith 快照对象将被调用,其中包含此更改的新快照,您可以将其应用于数据源。

      注意:您需要将managedObjectContext 属性添加到您的UITableViewDiffableDataSource 子类。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-11
        • 1970-01-01
        • 2012-09-22
        • 2014-11-21
        相关资源
        最近更新 更多