Skip to content

Instantly share code, notes, and snippets.

@thepost
Created December 4, 2018 03:55
Show Gist options
  • Save thepost/3de48077769714d549e3c8e8ae7d114a to your computer and use it in GitHub Desktop.
Save thepost/3de48077769714d549e3c8e8ae7d114a to your computer and use it in GitHub Desktop.
extension ModelController {
func delete(by objectID: NSManagedObjectID) {
let managedObject = context.object(with: objectID)
context.delete(managedObject)
}
func delete<M: NSManagedObject>(_ type: M.Type, predicate: NSPredicate?=nil) {
if let objects = fetch(type, predicate: predicate) {
for modelObject in objects {
delete(by: modelObject.objectID)
}
}
if context.deletedObjects.count > 0 {
save()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment