Deleting all data in a Core Data entity in Swift 3

后端 未结 3 1797
孤独总比滥情好
孤独总比滥情好 2021-01-31 15:58

Is there a way to do a batch delete of all data stored in all of the entities in core data?

I read somewhere that in iOS 9 or 10 that apple introduced a way to do batch

3条回答
  •  盖世英雄少女心
    2021-01-31 16:13

    To flesh out Tom's reply, this is what I added to have a complete routine:

    func deleteAllRecords() {
        let delegate = UIApplication.shared.delegate as! AppDelegate
        let context = delegate.persistentContainer.viewContext
    
        let deleteFetch = NSFetchRequest(entityName: "CurrentCourse")
        let deleteRequest = NSBatchDeleteRequest(fetchRequest: deleteFetch)
    
        do {
            try context.execute(deleteRequest)
            try context.save()
        } catch {
            print ("There was an error")
        }
    }
    

提交回复
热议问题