NSBatchDeleteRequest를 사용한 후 NSFetchedResultsControllerDelegate에서 정확한 업데이트를 얻는 데 문제가 있습니다. 변경 사항은 didChange 대리자 메서드의 삭제 유형이 아닌 업데이트 유형으로 전달됩니다. 변경 사항을 삭제로 가져 오는 방법이 있습니까? (삭제 대 업데이트에 대해 다른 시나리오가 있습니다.)NSBatchDeleteRequest를 사용하여 NSFetchedResultsController 사용
h 제 요청 : 가에
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)
}
}
인쇄 : 애플의 documentation에서
Have change with indexPath of: Optional([0, 0]), new index path of: Optional([0, 0]) for object: *my core data object*
update
부모 컨텍스트에서 삭제를 실행하고 제공된 개인 컨텍스트를 사용하여 삭제하려고한다고했습니다. – ELKA
@ELKA 가져온 결과 컨트롤러는 부모 컨텍스트를 사용하여 만들어지며 부모 컨텍스트는 부모가 컨텍스트이기 때문에 레코드를 삭제해야하는 영구 저장소가됩니다. –
그래서 context.parent는 NSFetchedResultController의 컨텍스트와 동일한 주 NSManagedObjectContext가됩니다. 옳은? – ELKA