Delete/Reset all entries in Core Data?

后端 未结 30 3155
天命终不由人
天命终不由人 2020-11-22 16:00

Do you know of any way to delete all of the entries stored in Core Data? My schema should stay the same; I just want to reset it to blank.


Edit

30条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 16:17

    Swift 5.1 Solution

    public static func reset() {
        let coordinator = _persistentContainer.persistentStoreCoordinator
        for store in coordinator.persistentStores where store.url != nil {
            try? coordinator.remove(store)
            try? FileManager.default.removeItem(atPath: store.url!.path)
        }
    }
    

提交回复
热议问题