仅为特定 table 维护领域数据迁移
Maintain Realm data migration for only specific table
我想用Realm migration
维护一个table的数据。例如这里,我想维护 Profile User
table 的数据并删除 table 的所有其余数据。
这是我到目前为止所做的。但是数据库中的所有内容都被删除了。
initRealm()
从 onCreate()
调用
private void initRealm() {
Realm.init(this);
RealmConfiguration configuration = new RealmConfiguration.Builder()
.schemaVersion(3)
.name("sipradi")
.migration(new RealmMigrationClass())
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(configuration);
}
配置class
private class RealmMigrationClass implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
if (oldVersion == 2) {
RealmObjectSchema profileUserSchema = schema.get("ProfileUser");
profileUserSchema.addField("test", String.class);
oldVersion++;
}
}
}
这将删除除 UserProfile 之外的所有数据 class
private void initRealm() {
Realm.init(this);
RealmConfiguration configuration = new RealmConfiguration.Builder()
.schemaVersion(3)
.name("sipradi")
.migration(new RealmMigrationClass())
.build();
Realm.setDefaultConfiguration(configuration);
}
private class RealmMigrationClass implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
if (oldVersion == 2) {
RealmObjectSchema profileUserSchema = schema.get("ProfileUser");
profileUserSchema.addField("test", String.class);
// Delete all other data than `ProfileUser`
for (RealmObjectSchema classSchema : schema.getAll()) {
if (classSchema.getClassName().equals("ProfileUser")) {
continue;
}
realm.delete(classSchema.getClassName());
}
oldVersion++;
}
}
}
我想用Realm migration
维护一个table的数据。例如这里,我想维护 Profile User
table 的数据并删除 table 的所有其余数据。
这是我到目前为止所做的。但是数据库中的所有内容都被删除了。
initRealm()
从 onCreate()
private void initRealm() {
Realm.init(this);
RealmConfiguration configuration = new RealmConfiguration.Builder()
.schemaVersion(3)
.name("sipradi")
.migration(new RealmMigrationClass())
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(configuration);
}
配置class
private class RealmMigrationClass implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
if (oldVersion == 2) {
RealmObjectSchema profileUserSchema = schema.get("ProfileUser");
profileUserSchema.addField("test", String.class);
oldVersion++;
}
}
}
这将删除除 UserProfile 之外的所有数据 class
private void initRealm() {
Realm.init(this);
RealmConfiguration configuration = new RealmConfiguration.Builder()
.schemaVersion(3)
.name("sipradi")
.migration(new RealmMigrationClass())
.build();
Realm.setDefaultConfiguration(configuration);
}
private class RealmMigrationClass implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
if (oldVersion == 2) {
RealmObjectSchema profileUserSchema = schema.get("ProfileUser");
profileUserSchema.addField("test", String.class);
// Delete all other data than `ProfileUser`
for (RealmObjectSchema classSchema : schema.getAll()) {
if (classSchema.getClassName().equals("ProfileUser")) {
continue;
}
realm.delete(classSchema.getClassName());
}
oldVersion++;
}
}
}