Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSBatchDeleteRequest in Swift 3 for iOS 10 only?

Tags:

xcode

ios

swift

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")
like image 608
ardevd Avatar asked Oct 07 '16 19:10

ardevd


2 Answers

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

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "ChargerPrimary")
like image 146
pbasdf Avatar answered Oct 24 '22 03:10

pbasdf


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

like image 42
Jitendra Modi Avatar answered Oct 24 '22 02:10

Jitendra Modi