添加多个配置/永久存储到核心数据swift

问题描述:

嗨,我跟着http://commandshift.co.uk/blog/2013/06/06/multiple-persistent-stores-in-core-data/教程,并创建了多个核心数据配置。我的一些实体位于配置“SQLStorage”中,有些位于“InMemory”中。添加多个配置/永久存储到核心数据swift

我希望SQLStorage配置可以利用NSSQLiteStoreType和InMemory配置来利用NSInMemoryStoreType。所以这里是我如何修改我的persistentStoreCoordinator获得者。

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: error: -addPersistentStoreWithType:InMemory configuration: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) ... returned error Error Domain=NSCocoaErrorDomain Code=134080 "(null)" UserInfo={NSUnderlyingException=Can't add the same store twice} with userInfo dictionary { NSUnderlyingException = "Can't add the same store twice"; }

2016-08-16 02:25:05.181 EncryptedCoredata[9457:461607] Unresolved error Error Domain=YOUR_ERROR_DOMAIN Code=9999 "Failed to initialize the application's saved data" UserInfo={NSLocalizedDescription=Failed to initialize the application's saved data, NSLocalizedFailureReason=There was an error creating or loading the application's saved data., NSUnderlyingError=0x7ff25a840ba0 {Error Domain=NSCocoaErrorDomain Code=134080 "(null)" UserInfo={NSUnderlyingException=Can't add the same store twice}}}, [NSLocalizedDescription: Failed to initialize the application's saved data, NSLocalizedFailureReason: There was an error creating or loading the application's saved data., NSUnderlyingError: Error Domain=NSCocoaErrorDomain Code=134080 "(null)" UserInfo={NSUnderlyingException=Can't add the same store twice}]

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

+0

@汤姆哈灵顿:请看看这个问题:)我找到了你的答案在这里类似的问题的问题:http://*.com/questions/33483051/coredata-multiple-persistent-stores –

即时错误是因为您没有更改2个块之间的URL,因此您试图在相同的路径上添加2个不同的商店,而这是您无法做到的。

+0

谢谢你帮助,但有点困惑先生!我在同一个模型中有单核数据模型和多种配置。所以URL永远是一样的正确??我怎样才能改变它们?你的意思是我应该为每个配置创建一个不同的模型? –