Ste*_*dez 7 migration android realm realm-list realm-migration
我正在使用Realm v0.80.1,我正在尝试为我添加的新属性编写迁移代码.该房产是RealmList.我不知道如何正确添加新列或设置一个值.
我有:customRealmTable.addColumn(,"list");
一旦正确添加了列,我将如何设置list属性的初始值?我想做的事情如下:
customRealmTable.setRealmList(newColumnIndex,rowIndex,new RealmList <>());
Vic*_*ani 14
从Realm v1.0.0开始(也许之前),你可以简单地调用RealmObjectSchema#addRealmListField(String, RealmObjectSchema)(链接到javadoc)来实现这一点.例如,如果您尝试向类中添加permissions类型字段,则RealmList<Permission>需要User编写:
if (!schema.get("User").hasField("permissions")) {
schema.get("User").addRealmListField("permissions", schema.get("Permission"));
}
Run Code Online (Sandbox Code Playgroud)
也有在领域的迁移文档的例子在这里.addRealmListField为方便起见,这里是完整的javadoc :
/**
* Adds a new field that references a {@link RealmList}.
*
* @param fieldName name of the field to add.
* @param objectSchema schema for the Realm type being referenced.
* @return the updated schema.
* @throws IllegalArgumentException if the field name is illegal or a field with that name already exists.
*/
Run Code Online (Sandbox Code Playgroud)
您可以在此处的示例中看到添加RealmList属性的示例:https://github.com/realm/realm-java/blob/master/examples/migrationExample/src/main/java/io/realm/examples/realmmigrationexample /model/Migration.java#L78-L78
相关代码是此部分:
if (version == 1) {
Table personTable = realm.getTable(Person.class);
Table petTable = realm.getTable(Pet.class);
petTable.addColumn(ColumnType.STRING, "name");
petTable.addColumn(ColumnType.STRING, "type");
long petsIndex = personTable.addColumnLink(ColumnType.LINK_LIST, "pets", petTable);
long fullNameIndex = getIndexForProperty(personTable, "fullName");
for (int i = 0; i < personTable.size(); i++) {
if (personTable.getString(fullNameIndex, i).equals("JP McDonald")) {
personTable.getRow(i).getLinkList(petsIndex).add(petTable.add("Jimbo", "dog"));
}
}
version++;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8805 次 |
| 最近记录: |