Created
January 23, 2017 14:33
-
-
Save TheSwiftyCoder/d076576601366bbad81d3752b1cb47bb to your computer and use it in GitHub Desktop.
Deleting core data objects in Swift 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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