我有一些核心的是執行批量刪除請求:如何設置NSBatchDeleteRequest的NSBatchDeleteResult類型?
extension NSManagedObject: Clearable {
/// Clears all objects of this type in coreData
static func clearAll() {
let context = AppDelegate.sharedInstance()?.coreDataHelper.objectContext()
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: String(describing:self))
let batchDeleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
do {
if let unwrappedContext = context {
unwrappedContext.shouldDeleteInaccessibleFaults = true
let result = try unwrappedContext.execute(batchDeleteRequest) as? NSBatchDeleteResult
DLog("result \(result.debugDescription)")
switch result!.resultType {
case .resultTypeCount:
DLog("resultTypeCount")
case .resultTypeObjectIDs:
DLog("resultTypeObjectIDs")
case .resultTypeStatusOnly:
DLog("resultTypeStatusOnly")
}
if let objectIDArray = result?.result as? [NSManagedObjectID] {
let changes = [NSDeletedObjectsKey : objectIDArray]
NSManagedObjectContext.mergeChanges(fromRemoteContextSave: changes, into: [unwrappedContext])
}
try context?.save()
}
} catch let error as NSError {
DLog("Error removing : \(error), \(error.localizedDescription)")
}
}
}
的代碼工作正常,但批處理的刪除結果總是.resultTypeStatusOnly
文檔這裏https://developer.apple.com/library/content/featuredarticles/CoreData_Batch_Guide/BatchDeletes/BatchDeletes.html#//apple_ref/doc/uid/TP40016086-CH3-SW2標題更新您的下說:應用程序執行後是
,重要的是你通知應用程序,在 內存中的對象陳舊,需要刷新。
要做到這一點,結果類型必須是.resultTypeObjectIDs
,才能觸發部分。目前尚不清楚你是如何得到這些的。
如何設置結果的類型?
正確的語法是:'batchDeleteRequest.resultType = .resultTypeObjectIDs' – Koen