gpt4 book ai didi

ios - 如何设置 NSBatchDeleteRequest 的 NSBatchDeleteResult 类型?

转载 作者:可可西里 更新时间:2023-11-01 02:02:46 26 4
gpt4 key购买 nike

我有一些执行批量删除请求的核心:

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执行后更新您的应用程序标题下说

it is important that you notify the application that the objects in memory are stale and need to be refreshed.

为此,结果类型必须是 .resultTypeObjectIDs,才能触发 NSManagedObjectContext.mergeChanges(fromRemoteContextSave: changes, into: [unwrappedContext])部分。只是不清楚你是如何得到它的。

如何设置结果的类型?

最佳答案

您可以在 batchDeleteRequest 上设置 resultType 属性。所以,也来自 https://developer.apple.com/library/content/featuredarticles/CoreData_Batch_Guide/BatchDeletes/BatchDeletes.html :

When the executeRequest completes successfully, a response is received. That response can take one of two forms. The form of the response is deteremined by setting the resultType property on the NSBatchDeleteRequest. The default value is NSStatusOnlyResultType, which returns nothing. The other option is NSBatchDeleteObjectIDsResultType, which returns an array of NSManagedObjectID instances indicating which entities were deleted during the execution.

希望这对您有所帮助。祝你好运!

关于ios - 如何设置 NSBatchDeleteRequest 的 NSBatchDeleteResult 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45127886/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com