Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何删除PersistentStore并重新创建以添加新数据?_Ios_Swift_Core Data_Nspersistentstore - Fatal编程技术网

Ios 如何删除PersistentStore并重新创建以添加新数据?

Ios 如何删除PersistentStore并重新创建以添加新数据?,ios,swift,core-data,nspersistentstore,Ios,Swift,Core Data,Nspersistentstore,我在应用程序中使用CoreData数据库。我需要删除它(模型和所有数据),并在应用程序更新时重新创建它 要删除它,我使用destroyPersistentStore函数。 但是删除之后,我需要重新创建持久存储,用新数据填充它 这里是我的CoreDataManager课程: class CoreDataManager { static let sharedManager = CoreDataManager() private init() {} lazy var per

我在应用程序中使用CoreData数据库。我需要删除它(模型和所有数据),并在应用程序更新时重新创建它

要删除它,我使用
destroyPersistentStore
函数。 但是删除之后,我需要重新创建
持久存储
,用新数据填充它

这里是我的
CoreDataManager
课程:

class CoreDataManager {

    static let sharedManager = CoreDataManager()
    private init() {}

    lazy var persistentContainer: NSPersistentContainer = {

        let container = NSPersistentContainer(name: storeName)
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in

            if let error = error as NSError? {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        return container
    }()

   func resetCoreData(){

        guard let firstStoreURL = self.persistentContainer.persistentStoreCoordinator.persistentStores.first?.url else {
            print("Missing first store URL - could not destroy")
            return
        }

        do {
            try self.persistentContainer.persistentStoreCoordinator.destroyPersistentStore(at: firstStoreURL, ofType: NSSQLiteStoreType, options: nil)
        } catch  {
            print("Unable to destroy persistent store: \(error) - \(error.localizedDescription)")
        }
   }

 func recreateCoreData() {
        do {
             try self.persistentContainer.persistentStoreCoordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: storeName, at: firstStoreURL, options: nil)
         } catch {
             print("Unable to create persistent store: \(error) - \(error.localizedDescription)")
         }
  }
}
我的
重新创建CoreData
调用出错,因为该存储与创建时使用的存储不兼容

怎么了

编辑:

数据库模型在两个版本之间没有更改

错误:

Error Domain=NSCocoaErrorDomain Code=134020 "The model configuration used to open the store is incompatible with the one that was used to create the store."

这可能是因为调用
addPersistentStore
时参数
configurationName
导致的:

addPersistentStore(ofType: NSSQLiteStoreType, configurationName: storeName, ...)
配置名称不是存储名称,如果从现有存储中转储它,则会得到
PF\u DEFAULT\u configuration\u name

您可以从现有存储区(
firstStore.configurationName
)使用它,或者通过再次调用
persistentContainer.loadPersistentStores(…)
更简单一些


项目示例:

这可能是由于调用
addPersistentStore
时参数
configurationName
造成的:

addPersistentStore(ofType: NSSQLiteStoreType, configurationName: storeName, ...)
配置名称不是存储名称,如果从现有存储中转储它,则会得到
PF\u DEFAULT\u configuration\u name

您可以从现有存储区(
firstStore.configurationName
)使用它,或者通过再次调用
persistentContainer.loadPersistentStores(…)
更简单一些


项目示例:

如果您的CoreData模型在应用程序版本之间发生更改,您可能需要执行迁移。我的模型在两个版本之间没有更改。请发布错误,告知您数据存储不兼容。@Magnas我编辑了我的问题。您是否尝试使用persistentContainer上的
persistentStoreCoordinator.replacePersistentStore
?我在这里使用它:用于类似(但不完全相同)的删除/恢复场景。也,如果你的模型在应用程序版本之间没有变化,我可以问你为什么不简单地从应用程序商店中删除所有对象,并保留新版本的所有其他内容?如果你的CoreData模型在应用程序版本之间变化,你可能需要执行迁移。我的模型在两个版本之间没有变化。请发布告诉你数据的错误存储不兼容。@麦格纳斯我编辑了我的问题。您是否尝试在persistentContainer上使用
persistentStoreCoordinator.replacePersistentStore
?我在这里使用它:用于类似(但不完全相同)的删除/恢复场景。另外,如果你的模型在应用程序版本之间没有变化,我可以问你为什么不简单地从应用商店中删除所有对象,并保留新版本的所有其他内容?