Zygote Zygote - 10 months ago 195
iOS Question

NSBatchDeleteRequest in Swift 3 for iOS 10 only?

So, migrating my code to

Swift 3
has me a bit stuck. It seems
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
is only available in
iOS 10
and newer. If I define the
the following way, I get an error because it expect the
to has a
argument type:

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

Answer Source

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

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