【问题标题】:Realm migration when using non-default configurations使用非默认配置时的领域迁移
【发布时间】:2017-05-14 18:18:41
【问题描述】:

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

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

是否存在我想念的这种东西?或者我还有其他方法可以解决这个问题吗?

【问题讨论】:

    标签: ios swift realm


    【解决方案1】:

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

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

    https://realm.io/docs/swift/latest/#realms 的文档中了解更多信息

    【讨论】:

      猜你喜欢
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 2017-06-03
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多