Ios 使用UIDIffableDataSource tableview删除项时,不会调用NFetchedResultsController委托方法didChangeContentWith

Ios 使用UIDIffableDataSource tableview删除项时,不会调用NFetchedResultsController委托方法didChangeContentWith,ios,swift,nsfetchedresultscontroller,diffabledatasource,nsdiffabledatasourcesnapshot,Ios,Swift,Nsfetchedresultscontroller,Diffabledatasource,Nsdiffabledatasourcesnapshot,我正在尝试使用UITableViewDiffableDataSource实现现有的coredata项目。我的tableview使用NSFetchedResultsController和相应的委托方法进行耦合。 我能够使用diffabledatasource在tableview中列出数据。我的数据源是用泛型类型声明的,如下所示 UITableViewDiffableDataSource<String, NSManagedObjectID> override func tableVie

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

UITableViewDiffableDataSource<String, NSManagedObjectID>
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) 
func控制器(控制器:NSFetchedResultsController,didChangeContentWith snapshot:NSDiffableDataSourceSnapshotReference)

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

而不是在
UITableViewDiffableDataSource
实现中重写
tableView(u:commit:)

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

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

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

- (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
子类中