iOS: Delete ALL Core Data Swift

前端 未结 23 685
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 11:20

I am a little confused as to how to delete all core data in swift. I have created a button with an IBAction linked. On the click of the button I have the follow

23条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 11:48

    Swift 4 :

    destroyPersistentStoreAtURL(_:withType:options:) will delete (or truncate) the target persistent store. This will safely destroy a persistent store.

    Add:

    do {
        try persistentStoreCoordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: persistentStoreURL, options: nil)
    } catch {
        // Error Handling
    }
    

    Destroy / Delete / Truncate:

    do {
        try persistentStoreCoordinator.destroyPersistentStoreAtURL(persistentStoreURL, withType: NSSQLiteStoreType, options: nil)
    } catch {
        // Error Handling
    }
    

    Note : Parameters in above method should be identical to addPersistentStoreWithType method. You need to re-initiate storeCoordinator to use store again.

提交回复
热议问题