Skip to content

Instantly share code, notes, and snippets.

@TheSwiftyCoder
Created January 23, 2017 14:33
Show Gist options
  • Save TheSwiftyCoder/d076576601366bbad81d3752b1cb47bb to your computer and use it in GitHub Desktop.
Save TheSwiftyCoder/d076576601366bbad81d3752b1cb47bb to your computer and use it in GitHub Desktop.
Deleting core data objects in Swift 3
fileprivate func getContext() -> NSManagedObjectContext {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
return appDelegate.persistentContainer.viewContext
}
func deleteTestObjects(entity: String) {
let context = getContext()
let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: entity)
fetch.returnsObjectsAsFaults = false
fetch.includesPropertyValues = false
let request = NSBatchDeleteRequest(fetchRequest: fetch)
do {
try context.execute(request)
print("Objects have been deleted")
} catch let error as NSError {
print("Error deleting objects \(error.localizedDescription)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment