领域数据库在迁移期间插入行
Realm Database insert rows during migration
我有一个小的迁移 class,我不想在应用程序启动时创建带有一些预定义行的 SportTypes table。我尝试了 migrate 方法下的评论解决方案,但没有机会。
public class SportTypes extends RealmObject {
@PrimaryKey
private Integer id;
private String name;
private RealmList<SportTypes> sportTypes;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public RealmList<SportTypes> getSportTypes() {
return sportTypes;
}
public void setSportTypes(RealmList<SportTypes> sportTypes) {
this.sportTypes = sportTypes;
}
}
public class RealmMigrations implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
schema.create("SportTypes")
.addField("id", Integer.class, FieldAttribute.PRIMARY_KEY)
.addField("name", String.class);
// schema.get("SportTypes")
// .transform(new RealmObjectSchema.Function() {
// @Override
// public void apply(DynamicRealmObject obj) {
// Realm realm = Realm.getDefaultInstance();
// realm.beginTransaction();
// SportTypes sport = realm.createObject(SportTypes.class);
// sport.setId(0);
// sport.setName("cycling");
// sport.getSportTypes().add(sport);
// realm.commitTransaction();
// realm.close();
// }
// });
// schema.get("SportTypes").transform(new RealmObjectSchema.Function() {
// @Override
// public void apply(DynamicRealmObject obj) {
// obj.setInt("id", 0);
// obj.setInt("id", 1);
// obj.setInt("id", 2);
// obj.setInt("id", 3);
// obj.setInt("id", 4);
// obj.setString("name", "Running");
// obj.setString("name", "Cycling");
// obj.setString("name", "Swimming");
// obj.setString("name", "IndoorRunning");
// obj.setString("name", "IndoorCycling");
// }
// });
// Deliberately not using old version to run upper lines
if (oldVersion == 0) {
oldVersion++;
}
}
}
我在 activity 的 onCreate 方法中调用了以下部分。我还手动更改版本以触发迁移的迁移事件 class 但无法获取任何行。
Realm.init(this);
final RealmConfiguration configuration = new RealmConfiguration.Builder().name("sports1761.realm").schemaVersion(1).migration(new RealmMigrations()).build();
Realm.setDefaultConfiguration(configuration);
// try {
// Realm.migrateRealm(configuration);
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
Realm realm = Realm.getDefaultInstance();
RealmObjectSchema schema = realm.getSchema().get("SportTypes");
final RealmResults<SportTypes> sports = realm.where(SportTypes.class).findAll();
Integer size = sports.size();
我发现 solution.You 应该首先获取架构并创建 DynamicRealmObject 并在添加新记录之前删除旧记录,然后只添加新记录。如果您的对象有主键,您应该使用 createObject 方法的字段和主键重载来创建对象。
public class RealmMigrations implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
if (oldVersion == 0) {
RealmSchema schema = realm.getSchema();
RealmObjectSchema sportTypesSchema = schema.get("SportTypes");
realm.delete("SportTypes");
DynamicRealmObject sportType = realm.createObject("SportTypes", 0);
sportType.setString("name", "Running");
DynamicRealmObject sportType1 = realm.createObject("SportTypes", 1);
sportType.setString("name", "Cycling");
DynamicRealmObject sportType2 = realm.createObject("SportTypes", 2);
sportType.setString("name", "Swimming");
DynamicRealmObject sportType3 = realm.createObject("SportTypes", 3);
sportType.setString("name", "Indoor Running");
DynamicRealmObject sportType4 = realm.createObject("SportTypes", 4);
sportType.setString("name", "Indoor Cycling");
RealmList<DynamicRealmObject> listItems = sportType.getList("sportTypes");
List<DynamicRealmObject> values = Arrays.asList(sportType, sportType1, sportType2, sportType3, sportType4);
listItems.addAll(values);
oldVersion++;
}
}
}
我有一个小的迁移 class,我不想在应用程序启动时创建带有一些预定义行的 SportTypes table。我尝试了 migrate 方法下的评论解决方案,但没有机会。
public class SportTypes extends RealmObject {
@PrimaryKey
private Integer id;
private String name;
private RealmList<SportTypes> sportTypes;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public RealmList<SportTypes> getSportTypes() {
return sportTypes;
}
public void setSportTypes(RealmList<SportTypes> sportTypes) {
this.sportTypes = sportTypes;
}
}
public class RealmMigrations implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
schema.create("SportTypes")
.addField("id", Integer.class, FieldAttribute.PRIMARY_KEY)
.addField("name", String.class);
// schema.get("SportTypes")
// .transform(new RealmObjectSchema.Function() {
// @Override
// public void apply(DynamicRealmObject obj) {
// Realm realm = Realm.getDefaultInstance();
// realm.beginTransaction();
// SportTypes sport = realm.createObject(SportTypes.class);
// sport.setId(0);
// sport.setName("cycling");
// sport.getSportTypes().add(sport);
// realm.commitTransaction();
// realm.close();
// }
// });
// schema.get("SportTypes").transform(new RealmObjectSchema.Function() {
// @Override
// public void apply(DynamicRealmObject obj) {
// obj.setInt("id", 0);
// obj.setInt("id", 1);
// obj.setInt("id", 2);
// obj.setInt("id", 3);
// obj.setInt("id", 4);
// obj.setString("name", "Running");
// obj.setString("name", "Cycling");
// obj.setString("name", "Swimming");
// obj.setString("name", "IndoorRunning");
// obj.setString("name", "IndoorCycling");
// }
// });
// Deliberately not using old version to run upper lines
if (oldVersion == 0) {
oldVersion++;
}
}
}
我在 activity 的 onCreate 方法中调用了以下部分。我还手动更改版本以触发迁移的迁移事件 class 但无法获取任何行。
Realm.init(this);
final RealmConfiguration configuration = new RealmConfiguration.Builder().name("sports1761.realm").schemaVersion(1).migration(new RealmMigrations()).build();
Realm.setDefaultConfiguration(configuration);
// try {
// Realm.migrateRealm(configuration);
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
Realm realm = Realm.getDefaultInstance();
RealmObjectSchema schema = realm.getSchema().get("SportTypes");
final RealmResults<SportTypes> sports = realm.where(SportTypes.class).findAll();
Integer size = sports.size();
我发现 solution.You 应该首先获取架构并创建 DynamicRealmObject 并在添加新记录之前删除旧记录,然后只添加新记录。如果您的对象有主键,您应该使用 createObject 方法的字段和主键重载来创建对象。
public class RealmMigrations implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
if (oldVersion == 0) {
RealmSchema schema = realm.getSchema();
RealmObjectSchema sportTypesSchema = schema.get("SportTypes");
realm.delete("SportTypes");
DynamicRealmObject sportType = realm.createObject("SportTypes", 0);
sportType.setString("name", "Running");
DynamicRealmObject sportType1 = realm.createObject("SportTypes", 1);
sportType.setString("name", "Cycling");
DynamicRealmObject sportType2 = realm.createObject("SportTypes", 2);
sportType.setString("name", "Swimming");
DynamicRealmObject sportType3 = realm.createObject("SportTypes", 3);
sportType.setString("name", "Indoor Running");
DynamicRealmObject sportType4 = realm.createObject("SportTypes", 4);
sportType.setString("name", "Indoor Cycling");
RealmList<DynamicRealmObject> listItems = sportType.getList("sportTypes");
List<DynamicRealmObject> values = Arrays.asList(sportType, sportType1, sportType2, sportType3, sportType4);
listItems.addAll(values);
oldVersion++;
}
}
}