使用 RealmMigration 在领域 Android 中将数据类型从字符串更改为长整型
Change Data Type from String to Long in Realm Android using RealmMigration
final RealmObjectSchema customerSchema = schema.get("Customer");
customerSchema.removeField("creditPeriod")
.addField("creditPeriod", Long.class);
以上是我用于领域迁移的代码。我删除了以前是字符串的现有字段,然后添加了相同的字段,在迁移代码和 Pojo.class
中更改了其数据类型
下面我刚刚提到了使用@christian-melchior
评论中提到的示例将字段类型从String迁移到int的示例
public class DataMigration implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
......
// Modify your check according to your version update.
if (oldVersion == 1) {
// Below I am going to change the type of field 'id' from 'String' to 'int'
// Get Schema of a Particular Class. Change --Testing-- with the respected pojo class name
RealmObjectSchema schema = schema.get("Testing");
// Add a new temporary field with the new type which you want to migrate. 'id_tmp' is a temporary integer field.
schema.addField("id_tmp", int.class);
// Set the previous value to the new temporary field
schema.transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
// Implement the functionality to change the type. Below I just transformed a string type to int type by casting the value. Implement your methodology below.
String id = obj.getString("id");
obj.setInt("id_tmp", Integer.valueOf(id));
}
});
// Remove the existing field
schema.removeField("id");
// Rename the temporary field which hold the transformed value to the field name which you insisted to migrate.
schema.renameField("id_tmp", "id");
oldVersion++;
}
......
}
}
Don't forgot to update the schema version and refer the above migration class instance in the RealmConfiguration
instance of realm.
final RealmObjectSchema customerSchema = schema.get("Customer");
customerSchema.removeField("creditPeriod")
.addField("creditPeriod", Long.class);
以上是我用于领域迁移的代码。我删除了以前是字符串的现有字段,然后添加了相同的字段,在迁移代码和 Pojo.class
中更改了其数据类型下面我刚刚提到了使用@christian-melchior
评论中提到的示例将字段类型从String迁移到int的示例public class DataMigration implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
......
// Modify your check according to your version update.
if (oldVersion == 1) {
// Below I am going to change the type of field 'id' from 'String' to 'int'
// Get Schema of a Particular Class. Change --Testing-- with the respected pojo class name
RealmObjectSchema schema = schema.get("Testing");
// Add a new temporary field with the new type which you want to migrate. 'id_tmp' is a temporary integer field.
schema.addField("id_tmp", int.class);
// Set the previous value to the new temporary field
schema.transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
// Implement the functionality to change the type. Below I just transformed a string type to int type by casting the value. Implement your methodology below.
String id = obj.getString("id");
obj.setInt("id_tmp", Integer.valueOf(id));
}
});
// Remove the existing field
schema.removeField("id");
// Rename the temporary field which hold the transformed value to the field name which you insisted to migrate.
schema.renameField("id_tmp", "id");
oldVersion++;
}
......
}
}
Don't forgot to update the schema version and refer the above migration class instance in the
RealmConfiguration
instance of realm.