Zygote Zygote - 1 month ago 49
iOS Question

NSBatchDeleteRequest in Swift 3 for iOS 10 only?

So, migrating my code to

Swift 3
has me a bit stuck. It seems
NSBatchDeleteRequest
requires
iOS 10
now? The only way I could make the code build is with the following snippet:

func removeAllChargerData(){
// Remove all charging data from persistent storage
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = ChargerPrimary.fetchRequest()
let entity = NSEntityDescription.entity(forEntityName: "ChargerPrimary", in: self.secondMoc)
fetchRequest.entity = entity
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)

do {
try self.secondMoc.execute(deleteRequest)
} catch {
let deleteError = error as NSError
NSLog("\(deleteError), \(deleteError.localizedDescription)")
}

}


However, a warning shows up indicating that
fetchRequest()
is only available in
iOS 10
and newer. If I define the
fetchRequest
the following way, I get an error because it expect the
fetchRequest
to has a
NSFetchRequestResult
argument type:

let fetchRequest = NSFetchRequest<ChargerPrimary>(entityName: "ChargerPrimary")

Answer

You just need to specify the correct type for the generic:

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "ChargerPrimary")
Comments