iOS: Delete ALL Core Data Swift

前端 未结 23 710
爱一瞬间的悲伤
爱一瞬间的悲伤 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:47

    An alternative can be to completely remove and recreate persistent store (iOS 10+, swift 3).

    let urls = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask);
        var dbUrl = urls[urls.count-1];
        dbUrl = dbUrl.appendingPathComponent("Application Support/nameOfYourCoredataFile.sqlite")
        do {
            try persistentContainer.persistentStoreCoordinator.destroyPersistentStore(at: dbUrl, ofType: NSSQLiteStoreType, options: nil);
        } catch {
            print(error);
        }
        do {
            try persistentContainer.persistentStoreCoordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: dbUrl, options: nil);
        } catch {
            print(error);
        }
    

提交回复
热议问题