gpt4 book ai didi

ios - 'NSBatchDeleteRequest' 仅适用于 iOS 9.0 或更新版本

转载 作者:行者123 更新时间:2023-11-28 14:34:37 25 4
gpt4 key购买 nike

我遇到了这个问题

'NSBatchDeleteRequest' is only available on iOS 9.0 or newer

class func delete(placeId:Int64) {
let context = CoreDataStack.getContext()
let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Place")
fetch.predicate = NSPredicate(format: "id = \(placeId)")
let request = NSBatchDeleteRequest(fetchRequest: fetch)

do {
_ = try context.execute(request)
} catch {
fatalError("Failed to execute request: \(error)")
}
CoreDataStack.saveContext()
}

在 iOS 9 之前使用的相同功能的代码是什么?

最佳答案

只是一个循环,获取记录并删除它们

let context = CoreDataStack.getContext()
let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Place")
fetch.predicate = NSPredicate(format: "id = \(placeId)")
let fetchResults = try context.fetch(request)
for anItem in fetchResults {
context.delete(anItem)
}

您可以添加此if 子句以考虑两种方式

if #available(iOS 9, macOS 10.11, *) {

关于ios - 'NSBatchDeleteRequest' 仅适用于 iOS 9.0 或更新版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50987454/

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