简体   繁体   English

“ NSBatchDeleteRequest”仅在iOS 9.0或更高版本上可用

[英]'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.

相关问题 centerXAnchor仅在iOS 9.0或更高版本中可用? - centerXAnchor is only available in iOS 9.0 or newer? &#39;setNewsstandIconImage&#39;仅在iOS 9.0或更高版本上可用 - 'setNewsstandIconImage' is only available on iOS 9.0 or newer Xcode UI测试:lldb错误“仅适用于iOS 9.0或更高版本” - Xcode UI Testing: lldb error “only available on iOS 9.0 or newer” “ SFSafariViewController”仅在iOS 9.0或更高版本(React Native Build)上可用 - 'SFSafariViewController' is only available on iOS 9.0 or newer (React Native Build) XCUIApplication仅适用于iOS 9.0或更高版本的swift3 - XCUIApplication is only available on iOS 9.0 or newer, swift3 仅适用于iOS 10的Swift 3中的NSBatchDeleteRequest? - NSBatchDeleteRequest in Swift 3 for iOS 10 only? &#39;LABiometryType&#39;仅适用于iOS 11.0.1或更高版本 - 'LABiometryType' is only available on iOS 11.0.1 or newer “ AppDelegate”仅在iOS 10.0或更高版本上可用 - 'AppDelegate' is only available on iOS 10.0 or newer Searchcontroller仅在iOS 11或更高版本上可用 - Searchcontroller only available on iOS 11 or newer &#39;NSPersistentContainer&#39;仅适用于iOS 10.0或更高版本 - 'NSPersistentContainer' is only available on iOS 10.0 or newer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM