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")
You just need to specify the correct type for the generic:
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "ChargerPrimary")
Probably It's also working for me in IOS 10
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "ChargerPrimary")
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
You should use fetchrequest like this without giving specific type to varible
Here is DEMO For IOS 9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With