15

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")
3
  • i have the same confusion too. Unfortunately no documentation from apple on that - guess they were too busy delivering all these new neat features, so understandable. Oct 12, 2016 at 7:23
  • Still, its a pretty significant feature that's not seemingly left completely broken. :/ And considering the amount of time Swift 3 and Xcode 8 spent in beta, I cant really see it's an understandable omission.
    – ardevd
    Oct 12, 2016 at 17:49
  • Your issue is just the generic fetch request, not the batch deletion as such
    – Wain
    Oct 24, 2016 at 6:43

2 Answers 2

9
+100

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

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "ChargerPrimary")
2
  • But can you use that with NSBatchDeleteRequest in iOS 9? I suspect the answer is now, which is the actual question here: How to use NSBatchDeleteRequest in Swift 3 with iOS 9.
    – ardevd
    Oct 24, 2016 at 9:00
  • @Zygote I have just tested on the iOS9.3 simulator, and it seems to work fine. What problems are you having with it?
    – pbasdf
    Oct 24, 2016 at 13:27
2

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

3
  • Yeah, If you want documentation then I will provide you a link Oct 24, 2016 at 9:00
  • @Zygote Did you get you want from that link ? Oct 24, 2016 at 9:07
  • I'll have to verify it later, but I suspect the link you posted is for Swift 2 since it's from 2015. Just to clarify: NSBatchDeleteRequest worked fine for me with Swift 2, but in Swift 3 I can only seem to get it to work for iOS 10.
    – ardevd
    Oct 24, 2016 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.