【问题标题】:How to use two different realm configurations如何使用两种不同的领域配置
【发布时间】:2017-05-24 11:05:51
【问题描述】:

我正在使用包含一些模式的默认领域实例。现在,我想在不同的领域实例中创建一个新模式。所以我创建了一个新的 Realm 配置,当我为这个新模式查询或存储实体时,我使用新配置,而不是默认配置。通过这种方式,我希望避免为新模式创建迁移的需要,因为我将它与不同的数据库一起使用。但是当我使用默认领域实例时,会抛出一个异常 (RealmMigrationNeededException),它告诉我必须为我的新模式创建迁移。如果我只想在新数据库中使用我的新架构,我可以以任何方式避免这种情况吗?

【问题讨论】:

    标签: android realm realm-java


    【解决方案1】:

    查看 Realm 的文档: https://realm.io/docs/java/latest/#schemas

    您需要为不同的配置定义 2 个自定义模块。 例如,configB 只关心 Cat.class 架构。

    // Create my module A
    @RealmModule(classes = { Person.class, Dog.class })
    public class MyModule {
    }
    
    // Create the module B
    @RealmModule(classes = { Cat.class })
    public class MyOtherModule {
    }
    
    // Set the module in the RealmConfiguration to allow only classes defined by the module.
    RealmConfiguration configA = new RealmConfiguration.Builder()
      .modules(new MyModule())
      .name("A.realm")
      .build();
    
    RealmConfiguration configB = new RealmConfiguration.Builder()
      .modules(new MyOtherModule())
      .name("B.realm")
      .build();
    

    【讨论】:

    • 非常感谢!这就是我一直在寻找的。​​span>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    相关资源
    最近更新 更多