有没有办法在迁移过程中将JSON导入Realm?
问题描述:
我为我的应用使用领域,现在是在迁移过程中进行第一次数据更新的时候了。不幸的是,DynamicRealm实例上没有createAllFromJson。有没有办法以其他方式获取Realm或调用createAllFromJson?有没有办法在迁移过程中将JSON导入Realm?
答
该用例没有实用程序。如果你想在迁移过程中使用JSON,你应该为它创建一些代码。
答
似乎没有成为API的方式,这里是我能做到最好:
Realm.init(app);
RealmConfiguration configuration = new RealmConfiguration.Builder()
.schemaVersion(X)
.migration((realm, oldVersion, newVersion) -> {
// migration stuff
realmReImportNeeded = true; // static bool false by default
})
.initialData(realm -> {
importData(realm);
})
.build();
Realm.setDefaultConfiguration(configuration);
// Open immediately so migration is triggered
try (Realm realm = Realm.getDefaultInstance()) {
if (realmReImportNeeded) {
realm.executeTransaction(realm1 -> importData(realm1));
}
}