public class User extends RealmObject{
...
RealmList<Tags> userTags;
}
public class Tags extends RealmObject{
...
RealmList<Results> tagResults;
}
public class Results extends RealmObject{
...
}
在我的一项活动中,我希望从特定的用户获取所有标记以及所有包含结果的内容。例如:
user1 -> tag1 -> result1
-> result2
-> tag2 -> result3
-> result4
所需的结果应该是result1,result2,result3,result4。
现在我正在手动
...
RealmList<Results> tagResults = new RealmList<>;
RealmList<Tags> userTags= user1.getTags();
for (Tags tag : userTags)
{
tagResults.addAll(tag.getResults());
}
mRealmAdapter = new RealmResultsAdapter(this, tagResults);
如果不手动操作,还有其他方法可以获得所需的结果吗?
提前致谢
答案 0 :(得分:3)
编辑: Realm 3.5.0添加了反向关系,所以你应该这样做:
public class User extends RealmObject{
...
RealmList<Tags> userTags;
}
public class Tags extends RealmObject{
...
@LinkingObjects("userTags")
private final RealmResults<User> tagOfUsers = null;
RealmList<Results> tagResults;
}
public class Results extends RealmObject{
@LinkingObjects("tagResults")
private final RealmResults<Tags> resultsOfTags = null;
...
}
和
RealmResults<Tags> tags = realm.where(Tags.class).equalTo("tagOfUsers.userId", userId).findAll(); // has results
PRE-3.0.0答案:
由于我们还在等待inverse relationships (backlinks),我会像这样设置RealmObjects
public class User extends RealmObject{
...
@PrimaryKey
long id;
RealmList<Tags> userTags;
}
public class Tags extends RealmObject{
...
@PrimaryKey
long id;
User user;
@Index
long userId;
RealmList<Results> tagResults;
}
public class Results extends RealmObject {
User user;
@Index
long userId;
Tags tag;
@Index
long tagId;
...
}
然后
RealmResults<Results> results = realm.where(Results.class).equalTo("userId", userId).findAll();