我有一些具有这种结构的领域数据:
time.Time
我想从部门中选择一些成员并添加到选定的会议中。
public class Member extends RealmObject {
@PrimaryKey
private int id;
private String name;
private String image_url;
}
public class Department extends RealmObject {
@PrimaryKey
private int id;
// some another fields...
private RealmList<Member> members;
}
public class Meeting extends RealmObject {
@PrimaryKey
private int id;
private String title = "";
private RealmList<Member> members = new RealmList<>();
}
下一个Java代码:
//properties
int departmentId = 1;
int meetingId = 4;
Meeting meeting;
Department department;
Realm realm;
//declaration
realm = Realm.getDefaultInstance();
realm.beginTransaction();
meeting = realm.where(Meeting.class).equalTo("id", meetingId).findFirst();
department = realm.where(Department.class).equalTo("id", departmentId).findFirst();
//add members
for (Member member : department.getMembers()) {
if (!thereIsInList(member,meeting.getMembers()))
meeting.getMembers().add(member);
}
//realm update and close
realm.insertOrUpdate(meeting);
realm.commitTransaction();
realm.close();
但是我看到新成员与会议无关。 请帮我找出问题所在
答案 0 :(得分:1)
首先在realm studio中检查您要查找的ID是否正确,并且所需的列表是否存在于与相应ID相关的Realm中。下面的代码段处理了真实的交易
Realm realmObj;
//declaration
realmObj= Realm.getDefaultInstance();
realmObj.executeTransaction(realm -> {
meeting = realm.where(Meeting.class).equalTo("id", meetingId).findFirst();
department = realm.where(Department.class).equalTo("id", departmentId).findFirst();
//add members
for (Member member : department.getMembers()) {
if (!thereIsInList(member,meeting.getMembers()))
meeting.getMembers().add(member);
}
});