简体   繁体   English

Swift4:在两个核心数据存储之间切换

[英]Swift4: Switching between two Core Data stores

I have a requirement for two different CoreData database stores, depending on which mode my application is running in. It will only need to be switched when the application first runs. 我需要两个不同的CoreData数据库存储,具体取决于我的应用程序以哪种模式运行。仅在应用程序首次运行时才需要切换它。

Ideally this would get setup with the persistentContainer: 理想情况下,将使用persistentContainer进行设置:

lazy var persistentContainer: NSPersistentContainer = 
{
    let container = NSPersistentContainer(name: "myApp")
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? 
        {
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
}

I basically just need to find a command like: 我基本上只需要找到一个类似的命令:

if mode == 1
{
    storeName = "MyStore1.sqlite"
}
else
{
    storeName = "MyStore2.sqlite"
}
container.useStore(storeName)

You're on the right track. 您走在正确的轨道上。 In broad strokes, a typical configuration would include a single NSManagedObjectContext , which requires a NSPersistentStoreCoordinator , which in turn requires a NSManagedObjectModel . 概括地说,典型的配置将包括单个NSManagedObjectContext ,它需要一个NSPersistentStoreCoordinator ,而后者又需要一个NSManagedObjectModel The context can have another context above it, if you wish to have an intermediate step between your front end and the persistent store. 如果您希望在前端和持久性存储之间有一个中间步骤,则该上下文可以具有另一个上下文。

Either way, you have a couple options (and probably more): 无论哪种方式,您都有几个选择(可能还有更多):

  1. Add another store to your NSPersistentStoreCoordinator (see this answer ) 将另一个商店添加到您的NSPersistentStoreCoordinator (请参阅此答案
  2. If you also want two separate models for each store, create two different instances of NSManagedObjectContext , each with its own NSPersistentStoreCoordinator and NSManagedObjectModel . 如果您还希望为每个商店使用两个单独的模型,请创建两个NSManagedObjectContext实例,每个实例都有自己的NSPersistentStoreCoordinatorNSManagedObjectModel

Option #2 would be a bit clunky to manage in code so if you only worry about where the data is physically stored, I'd go with option #1. 选项#2在代码中管理起来有些笨拙,因此,如果您只担心数据的物理存储位置,我将选择选项#1。 Otherwise, you may want to create some kind of a context manager that will correctly oversee each context (you can also put your mode == 1 / model == 2 logic here) to make sure they don't step on each other's toes. 否则,您可能想要创建某种上下文管理器,以正确地监视每个上下文(您也可以在这里将mode == 1 / model == 2逻辑放在这里),以确保它们不会踩到对方的脚趾。

When you initialise a persistent container with init(name:) , the name is used to locate both the model and the persistent store. 当您使用init(name:)初始化持久性容器时,该名称将用于定位模型和持久性存储。 From the docs : 文档

By default, the provided name value is used to name the persistent store and is used to look up the name of the NSManagedObjectModel object to be used with the NSPersistentContainer object. 默认情况下,提供的名称值用于命名持久性存储,并用于查找要与NSPersistentContainer对象一起使用的NSManagedObjectModel对象的名称。

But there is another initialiser you can use, init(name:, managedObjectModel:) , which will allow you to specify separate names for the model and the store. 但是您可以使用另一个初始化程序init(name:, managedObjectModel:) ,它将允许您为模型和商店指定单独的名称。 From the docs : 文档

By default, the provided name value of the container is used as the name of the persisent store associated with the container. 默认情况下,提供的容器名称值用作与容器关联的持久性存储的名称。 Passing in the NSManagedObjectModel object overrides the lookup of the model by the provided name value. 传入NSManagedObjectModel对象将通过提供的名称值覆盖对模型的查找。

So you can achieve what you want just by specifying the correct name and managedObjectModel parameters when you initialise the persistent container. 因此,只需在初始化持久性容器时指定正确的名称和ManagedObjectModel参数即可实现所需的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM