我正在使用以下代码设置我的应用程序持久性容器:

lazy var persistentContainer: NSPersistentContainer = {
    let container = NSPersistentContainer(name: "App_Name")

    let myFileManager = FileManager()

    do {
        let docsurl = try myFileManager.url(for:.applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: false)

        let myUrl = docsurl.appendingPathComponent("App_Name")

        let description = NSPersistentStoreDescription(url: myUrl)
        container.persistentStoreDescriptions = [description]

        let options = [NSInferMappingModelAutomaticallyOption : true,
                        NSMigratePersistentStoresAutomaticallyOption : true]

        try container.persistentStoreCoordinator.addPersistentStore(ofType: NSInMemoryStoreType, configurationName: nil, at: myUrl, options: options)

    } catch {
        fatalErrorText = error.localizedDescription
        print(fatalErrorText)
    }

    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            fatalErrorText = error.debugDescription
            print(fatalErrorText)
        }
    })
    return container
}()


但是,当我尝试访问核心数据时,出现以下错误:


  2017-08-07 14:43:57.391529 + 0100应用程序名称[98764:1854740] [错误]错误:-addPersistentStoreWithType:SQLite配置:(空)URL:file:/// Users / Seb / Library / Developer / CoreSimulator /设备/ 241E1A36-631B-4071-8357-5F551F32403F /数据/容器/数据/应用程序/BC35D1CD-FA17-4F1F-99A0-EB0E73A42F3C/Library/Application%20Support/App_Name.sqlite选项:{
      NSInferMappingModelAutomaticallyOption = 1;
      NSMigratePersistentStoresAutomaticallyOption = 1;
  } ...返回错误Error Domain = NSCocoaErrorDomain代码= 134080“(null)” UserInfo = {NSUnderlyingException =无法两次添加同一商店}与userInfo字典{
      NSUnderlyingException =“不能两次添加相同的存储”
  }
  CoreData:错误:-addPersistentStoreWithType:SQLite配置:(null)URL:file:/// Users / Seb / Library / Developer / CoreSimulator / Devices / 241E1A36-631B-4071-8357-5F551F32403F / data / Containers / Data / Application / BC35D1CD-FA17-4F1F-99A0-EB0E73A42F3C / Library / Application%20Support / App_Name.sqlite选项:{
      NSInferMappingModelAutomaticallyOption = 1;
      NSMigratePersistentStoresAutomaticallyOption = 1;
  } ...返回错误Error Domain = NSCocoaErrorDomain代码= 134080“(null)” UserInfo = {NSUnderlyingException =无法两次添加同一商店}与userInfo字典{
      NSUnderlyingException =“不能两次添加相同的存储”
  }


我启用了iCloud,并确实找到了一个答案,声称该问题与iCloud有关,但他们的解决方案对我不起作用。

我在这里找到了针对此问题的其他一些解决方案,但还无法解读/翻译答案。

最佳答案

NSPersistentContainer是核心数据堆栈所需的所有包装。它将创建一个带有单个SQL存储的persistentStoreCoordinator设置的managedObjectContext。可以轻松找到具有您命名的模型的名称,并使用相同的名称命名sql文件。默认情况下,NSPersistentContainer的自动迁移功能处于打开状态。

如果需要更自定义的设置,则可以自己创建所有实体(这并不难)。或者,可以在调用persistentStoreDescriptions之前设置loadPersistentStores属性。使用persistentStoreDescription,您可以设置将sql文件保存到的URL,并设置shouldMigrateStoreAutomatically
 通常没有理由将其设置为默认值,请参见https://developer.apple.com/documentation/coredata/nspersistentstoredescription/1640566-shouldmigratestoreautomatically

TL; DR摆脱了do catch中的所有内容,loadPersistentStores的默认行为将起作用

关于ios - NSUnderlyingException =“无法两次添加同一存储”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45548756/

10-09 04:06