[英]'NSBatchDeleteRequest' is only available on iOS 9.0 or newer
I am facing this issue 我正面临这个问题
'NSBatchDeleteRequest' is only available on iOS 9.0 or newer “ NSBatchDeleteRequest”仅在iOS 9.0或更高版本上可用
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()
}
what was the code that is used prior to iOS 9 for same funtionality ? 在iOS 9之前用于相同功能的代码是什么?
Just a loop, fetch the records and delete them 只是循环,获取记录并删除它们
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)
}
You can add this if
clause to consider both ways 您可以添加此if
子句以同时考虑两种方式
if #available(iOS 9, macOS 10.11, *) {
Before iOS 9 you must fetch every record of the entity and mark it for deletion, then save the changes. 在iOS 9之前,您必须获取实体的每条记录并将其标记为删除,然后保存更改。
Something like this: 像这样:
context.performBlockAndWait
{
if let result = try? context.fetch(fetch)
{
for object in result
{
context.delete(object)
}
}
do
{
// After mark to delete you must save the context
try context.save()
}
catch
{
// Error
}
}
Thank to user1046037 helps. 感谢user1046037的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.