使用非默认配置时的域迁移

时间:2017-05-14 18:18:42

标签: ios swift realm

在领域文档中,我看到了迁移的代码示例

let config = Realm.Configuration(
  // Set the new schema version. This must be greater than the previously used
  // version (if you've never set a schema version before, the version is 0).
  schemaVersion: 1,

  // Set the block which will be called automatically when opening a Realm with
  // a schema version lower than the one set above
  migrationBlock: { migration, oldSchemaVersion in
    // We haven’t migrated anything yet, so oldSchemaVersion == 0
    if (oldSchemaVersion < 1) {
      // Nothing to do!
      // Realm will automatically detect new properties and removed properties
      // And will update the schema on disk automatically
    }
  })

// Tell Realm to use this new configuration object for the default Realm
Realm.Configuration.defaultConfiguration = config

然后它说使用let realm = try! Realm()来获取领域实例。但是,在我们的应用程序中,我们使用自己的领域配置,如

let realm = try! Realm(configuration: RealmConfig.getConfig(typeURL: .userData)!)

除了.userData之外,我们还有一些不同的配置。我的问题是,如何使用这些非默认配置进行迁移?代码示例实际上只显示了如何设置默认配置,这对我的使用来说无关紧要。我找不到像

这样的东西

Realm.Configuration.userData = config

我是否遗漏了这样的事情?或者我还有另一种方法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

您可以使用不同的配置实例化不同的Realm实例,如下所示:

let userDataConfiguration = Realm.Configuration(...)
let userDataRealm = try! Realm(configuration: userDataConfiguration)

https://realm.io/docs/swift/latest/#realms

的文档中了解详情