0

I am facing this issue

'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()
    }

what was the code that is used prior to iOS 9 for same funtionality ?

Varun Naharia
  • 5,318
  • 10
  • 50
  • 84

2 Answers2

1

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 #available(iOS 9, macOS 10.11, *) {
vadian
  • 274,689
  • 30
  • 353
  • 361
0

Before iOS 9 you must fetch every record of the entity and mark it for deletion, then save the changes.

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.

Kerberos
  • 4,036
  • 3
  • 36
  • 55