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")
Jitendra Modi
  • 2,344
  • 12
  • 34
ardevd
  • 3,329
  • 5
  • 30
  • 55
  • 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. – user1244109 Oct 12 '16 at 07: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 '16 at 17:49
  • Your issue is just the generic fetch request, not the batch deletion as such – Wain Oct 24 '16 at 06:43

2 Answers2

9

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

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "ChargerPrimary")
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
pbasdf
  • 21,386
  • 4
  • 43
  • 75
  • 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 '16 at 09: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 '16 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

Jitendra Modi
  • 2,344
  • 12
  • 34
  • Yeah, If you want documentation then I will provide you a link – Jitendra Modi Oct 24 '16 at 09:00
  • @Zygote Did you get you want from that link ? – Jitendra Modi Oct 24 '16 at 09: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 '16 at 09:29