RealmMigrationTests.java 文件源码

java
阅读 26 收藏 0 点赞 0 评论 0

项目:GitHub 作者:
@Test
public void migrationException_realmListChanged() throws IOException {
    RealmConfiguration config = configFactory.createConfiguration();
    // Initialize the schema with RealmList<Cat>
    Realm.getInstance(configFactory.createConfiguration()).close();

    DynamicRealm dynamicRealm = DynamicRealm.getInstance(config);
    dynamicRealm.beginTransaction();
    // Change the RealmList type to RealmList<Dog>
    RealmObjectSchema dogSchema = dynamicRealm.getSchema().get(Dog.CLASS_NAME);
    RealmObjectSchema ownerSchema = dynamicRealm.getSchema().get(CatOwner.CLASS_NAME);
    ownerSchema.removeField(CatOwner.FIELD_CATS);
    ownerSchema.addRealmListField(CatOwner.FIELD_CATS, dogSchema);
    dynamicRealm.commitTransaction();
    dynamicRealm.close();

    try {
        realm = Realm.getInstance(config);
        fail();
    } catch (RealmMigrationNeededException ignored) {
        assertThat(ignored.getMessage(),
                CoreMatchers.containsString("Property 'CatOwner.cats' has been changed from 'array<Dog>' to 'array<Cat>'"));
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号