我有一个 iOS 应用程序,我想在每次启动时都从一个新的核心数据数据库开始。存储类型是 SQLite。
但是,当我调用 persistenceStoreCoordinator.destroyPersistentStore() 时,100% 都会收到错误。
这是代码:
func destroyPersistentStore() {
guard let modelURL = Bundle.main.url(forResource: self.modelName, withExtension: "momd") else {
print("Missing data model - could not destroy")
return
}
do {
try persistentStoreCoordinator.destroyPersistentStore(at: modelURL, ofType: storeType, options: nil)
} catch {
print("Unable to destroy persistent store: \(error) - \(error.localizedDescription)")
}
}
错误是:
无法销毁持久存储:错误域=NSSQLiteErrorDomain
代码=14“(空)”UserInfo={NSFilePath=.../AppName.app/ModelName.momd,
Reason=无法截断数据库} - 操作无法
完全的。 (NSSQLiteErrorDomain 错误 14。)
即使发生此错误后,应用程序仍能够保存和访问商店中的数据。问题在于每次启动时都会加载初始数据,从而创建重复项。
以下是调用 destroyPersistentStore 时的情况:
- SQLite数据文件确实存在并且包含数据
- 发生在模拟器或真实设备上
- 型号Url正确,指向模具
- 存储类型是 SQLite
- SQLite数据文件保存在Documents目录中
- persistStoreCoordinator.url(for: permanentStoreCoordinator.persistentStores.first!) 指向 Documents 目录中的文件。
我已经在线搜索答案,但找不到任何人报告此错误,但我在这个项目和简化的演示项目中都有错误。我根本无法使 destroyPersistentStore 工作。
最后,当我暂停执行并 po 持久存储协调器.托管对象模型时,第一行是:
po persistenceStoreCoordinator.managedObjectModel
() isEditable 0,实体...
isEditable 问题可能是问题所在吗?我该如何改变它?