Last active
September 30, 2020 18:05
-
-
Save bishalg/0390007d52963a180eafd3120012c8e1 to your computer and use it in GitHub Desktop.
CoreData Helper to Delete All Entities
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
// | |
// CoreDataHelper.swift | |
// Xcode 8.0 | |
// Swift 3.0 | |
// Subclass of CoreDataStack - | |
// https://gist.github.com/bishalg/6e3a4fc4020b558f40b02c1d835aad8b | |
// | |
import Foundation | |
import CoreData | |
class CoreDataHelper: CoreDataStack { | |
// Singleton | |
static let shared = CoreDataHelper() | |
fileprivate override init() { } | |
func deleteAllEntities() { | |
let entities = managedObjectModel.entities | |
for entitie in entities { | |
debugPrint("Deleting Entitie - ", entitie.name) | |
delete(entityName: entitie.name!) | |
} | |
} | |
func delete(entityName: String) { | |
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: entityName) | |
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest) | |
do { | |
try persistentStoreCoordinator.execute(deleteRequest, with: managedObjectContext) | |
debugPrint("Deleted Entitie - ", entityName) | |
} catch let error as NSError { | |
debugPrint("Delete ERROR \(entityName)") | |
debugPrint(error) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment