领域数据库,添加对象数组列表作为 class 的列
Realm database, add a arraylist of objects as a column for a class
到目前为止,我使用 Realm 中的示例进行迁移。
为了添加一个简单的列,我会做类似的事情:
Table personTable = realm.getTable(Trip.class);
long vehicleIndex = personTable.addColumn(ColumnType.STRING, "vehicle");
但是现在我需要向对象添加一个像这样的数组列表:
private ArrayList<StopInfo> filteredLocations = new ArrayList<>();
我怎样才能解决这个迁移?
首先,在Realm中,如果你想要一个列表字段,RealmList is the thing to use. See doc。
其次,在您的迁移中添加一个 RealmList:
假设您在模型 class
中定义了 RealmList<StopInfo>
private RealmList<StopInfo> stopInfoList;
要将其添加到迁移中:
// In the Migration
// Create RealmList field
long listIndex = table.addColumnLink(ColumnType.LINK_LIST,
"stopInfoList", transaction.getTable("class_StopInfo"));
到目前为止,我使用 Realm 中的示例进行迁移。 为了添加一个简单的列,我会做类似的事情:
Table personTable = realm.getTable(Trip.class);
long vehicleIndex = personTable.addColumn(ColumnType.STRING, "vehicle");
但是现在我需要向对象添加一个像这样的数组列表:
private ArrayList<StopInfo> filteredLocations = new ArrayList<>();
我怎样才能解决这个迁移?
首先,在Realm中,如果你想要一个列表字段,RealmList is the thing to use. See doc。
其次,在您的迁移中添加一个 RealmList:
假设您在模型 class
中定义了RealmList<StopInfo>
private RealmList<StopInfo> stopInfoList;
要将其添加到迁移中:
// In the Migration
// Create RealmList field
long listIndex = table.addColumnLink(ColumnType.LINK_LIST,
"stopInfoList", transaction.getTable("class_StopInfo"));