【问题标题】:Adding Multiple Configurations/ Persistent Store to core data swift快速添加多个配置/持久存储到核心数据
【发布时间】:2016-12-22 03:04:06
【问题描述】:

您好,我按照http://commandshift.co.uk/blog/2013/06/06/multiple-persistent-stores-in-core-data/ 教程为核心数据创建了多个配置。我的一些实体在配置“SQLStorage”中,一些在“InMemory”中。

我希望 SQLStorage 配置使用NSSQLiteStoreType,而 InMemory 配置使用NSInMemoryStoreType。所以这就是我如何修改我的persistentStoreCoordinator getter。

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
        // The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
        // Create the coordinator and store
        let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
        let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite")
        var failureReason = "There was an error creating or loading the application's saved data."
        do {
            try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: "SQLStorage", URL: url, options: nil)
        } catch {
            // Report any error we got.
            var dict = [String: AnyObject]()
            dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
            dict[NSLocalizedFailureReasonErrorKey] = failureReason

            dict[NSUnderlyingErrorKey] = error as NSError
            let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
            // Replace this with code to handle the error appropriately.
            // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)")
            abort()
        }
        do {
            try coordinator.addPersistentStoreWithType(NSInMemoryStoreType, configuration: "InMemory", URL: url, options: nil)
        } catch {
            // Report any error we got.
            var dict = [String: AnyObject]()
            dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
            dict[NSLocalizedFailureReasonErrorKey] = failureReason

            dict[NSUnderlyingErrorKey] = error as NSError
            let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
            // Replace this with code to handle the error appropriately.
            // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)")
            abort()
        }

        return coordinator
    }()

超时我尝试编译这段代码我得到了错误:

CoreData:错误:-addPersistentStoreWithType:InMemory 配置:内存中 URL:file:///Users/developer/Library/Developer/CoreSimulator/Devices/1D943405-C5AD-4CD4-9413-070DFC5334AB/data/Containers/Data/Application/2BEAF446-6F32-405B-B2B6-6B298D45E103/Documents/SingleViewCoreData .sqlite options:(null) ... 返回错误 Error Domain=NSCocoaErrorDomain Code=134080 "(null)" UserInfo={NSUnderlyingException=无法添加 同一家商店两次} 与 userInfo 字典 { NSUnderlyingException = "不能两次添加同一个商店"; }

2016-08-16 02:25:05.181 EncryptedCoredata[9457:461607] 未解决 错误 错误域=YOUR_ERROR_DOMAIN 代码=9999 “初始化失败 应用程序保存的数据” UserInfo={NSLocalizedDescription=Failed 初始化应用程序保存的数据, NSLocalizedFailureReason=创建或加载 应用程序保存的数据。,NSUnderlyingError=0x7ff25a840ba0 {错误 域=NSCocoaErrorDomain 代码=134080 "(null)" UserInfo={NSUnderlyingException=不能两次添加同一个商店}}}, [NSLocalizedDescription:无法初始化应用程序的保存 数据,NSLocalizedFailureReason:创建或加载时出错 应用程序保存的数据。,NSUnderlyingError:错误 域=NSCocoaErrorDomain 代码=134080 "(null)" UserInfo={NSUnderlyingException=不能两次添加同一个商店}]

可能是什么问题?任何帮助,将不胜感激。提前致谢。

【问题讨论】:

标签: ios core-data


【解决方案1】:

直接错误是因为您没有更改 2 个 do 块之间的 URL,因此您尝试在同一路径上添加 2 个不同的商店,这是您无法做到的。

【讨论】:

  • 感谢您的帮助,但先生有点困惑!我在同一模型中有单核数据模型和多个配置。所以 URL 总是一样正确的??我怎样才能改变它们???你的意思是我应该为每个配置创建不同的模型??
  • @SandeepBhandari 有 2 个问题: 1 - 如上所述,您正在尝试将相同的 URL 设置为 2 个持久存储(具有相同文件的 2 个持久存储:“SingleViewCoreData.sqlite” - 这是不可能的) , 和 2 - NSInMemoryStoreType 不会将任何内容保存到磁盘,因此它的“InMemory”部分。如果您阅读以下链接,您将看到您无法存储第二个配置:lists.apple.com/archives/cocoa-dev/2008/Oct/msg01524.html
猜你喜欢
  • 2015-02-03
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 2012-12-15
  • 2011-01-16
  • 1970-01-01
  • 1970-01-01
  • 2012-12-20
相关资源
最近更新 更多