2

我在使用NSBatchDeleteRequest後從NSFetchedResultsControllerDelegate獲取準確更新的問題,更改作爲更新類型而不是didChange委託方法的刪除類型。有沒有辦法讓更改作爲刪除來進入? (我有不同的方案來刪除vs更新)。使用NSFetchedResultsController w/NSBatchDeleteRequest

刪除請求: (使用調用者提供私有上下文中)在

fileprivate class func deletePeopleWith(ids: Set<Int>, usingContext context: NSManagedObjectContext) { 

    let fr: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Person") 

    var predicates: [NSPredicate] = [] 
    ids.forEach { 

     predicates.append(NSPredicate(format: "id = %ld", $0)) 
    } 

    fr.predicate = NSCompoundPredicate(orPredicateWithSubpredicates: predicates) 

    let dr = NSBatchDeleteRequest(fetchRequest: fr) 
    dr.affectedStores = context.parent?.persistentStoreCoordinator?.persistentStores 

    do { 

     try context.parent?.execute(dr) 
     context.parent?.refreshAllObjects() 
     print("Deleting person") 

    } catch let error as NSError { 

     let desc = "Could not delete person with batch delete request, with error: \(error.localizedDescription)" 
     debugPrint(desc) 
    } 

} 

結果:

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) { 

    DispatchQueue.main.async { 

     let changeDesc = "Have change with indexPath of: \(indexPath), new index path of: \(newIndexPath) for object: \(anObject)" 
     var changeType = "" 
     switch type { 

     case .delete: 
      changeType = "delete" 

     case .insert: 
      changeType = "insert" 

     case .move: 
      changeType = "move" 

     case .update: 
      changeType = "update" 

     } 

     print(changeDesc) 
     print(changeType) 
    } 

} 

印刷:

Have change with indexPath of: Optional([0, 0]), new index path of: Optional([0, 0]) for object: *my core data object* 
update 
+0

您正在對父上下文執行刪除操作,並且您表示要使用提供的私有上下文進行刪除。 – ELKA

+0

@ELKA抓取的結果控制器是使用父上下文創建的,父上下文也是上下文,其父是持久存儲的應該刪除記錄的上下文。 –

+0

因此context.parent將是一個主NSManagedObjectContext,它與NSFetchedResultController的上下文相同。正確? – ELKA

回答

4

從蘋果公司的documentation

批處理刪除運行速度快於您自己在代碼中刪除Core Data實體的速度,因爲它們在SQL級別的持久性存儲本身中操作。作爲這種差異的一部分,持久性存儲上制定的更改不會反映在當前內存中的對象中。

執行批量刪除操作後,刪除已從持久存儲中刪除的內存中的所有對象。

請參閱Updating Your Application After Execution部分以處理NSBatchDeleteRequest刪除的對象。

+0

單獨的文檔無法正常工作,但將結果類型更新爲刪除請求上的對象ID,然後調用刷新結果控制器使用的上下文中的所有對象後,得到的更改作爲刪除進入! –

0

這是樣品,適合我。但我沒有找到如何更新與動畫刪除,因爲NSFetchedResultsControllerDelegate沒有開火。

@IBAction func didPressDelete(_ sender: UIBarButtonItem) { 
     let managedObjectContext = persistentContainer.viewContext 
     let request = NSFetchRequest<NSFetchRequestResult>(entityName: String(describing: Record.self)) 
     let deleteRequest = NSBatchDeleteRequest(fetchRequest: request) 

     do { 
      // delete on persistance store level 
      try managedObjectContext.execute(deleteRequest) 

      // reload the whole context 
      managedObjectContext.reset() 
      try self.fetchedResultsController.performFetch() 
      tableV.reloadData() 
     } catch { 
      print("") 
     } 
    } 

Thanks

相關問題