public class ChallengeDB extends RealmObject {
@PrimaryKey
private int challengeId;
private String targetType;
private RealmList<ChallengeTargetDB> ChallengeTargetDB;
private boolean isTargetPerParticipant;
}
public class ChallengeTargetDB extends RealmObject {
@PrimaryKey
private int targetId;
private String targetName;
private String description;
private long targetValue;
private int targetStep;
private boolean isPassed;
}
我将这两个表链接如下,我需要获得max&#34; targetValue&#34;特定挑战的价值。
我写的查询如下
RealmResults<ChallengeDB> challengeDBs = realm.where(ChallengeDB.class)
.equalTo(WorkoutCashConstants.COLUMN_CHALLENGE_ID, challengeDB.getChallengeId()).findAll();
long max = challengeDBs.max("ChallengeTargetDB.targetValue").longValue();
我起床结束异常如下
java.lang.IllegalArgumentException: Aggregates on child object fields are not supported: ChallengeTargetDB.targetValue
答案 0 :(得分:1)
使用Realm 3.5.0 +
public class ChallengeTargetDB extends RealmObject {
@PrimaryKey
private int targetId;
private String targetName;
private String description;
private long targetValue;
private int targetStep;
private boolean isPassed;
@LinkingObjects("ChallengeTargetDb")
private final RealmResults<ChallengeDb> targetOfChallenge = null;
}
然后
RealmResults<ChallengeTargetDB> challengeDBs = realm.where(ChallengeTargetDB.class)
.equalTo("targetOfChallenge." + WorkoutCashConstants.COLUMN_CHALLENGE_ID, challengeDB.getChallengeId()).findAll();
long max = challengeDBs.max("targetValue").longValue();
答案 1 :(得分:0)
如异常所述,不支持链接对象字段的聚合。你需要自己计算它。
试试这个:
Number max = null;
for (ChallengeDB item : challengeDBs) {
if (max == null) {
max = item.ChallengeTargetDB.max();
} else {
Number tmp = item.ChallengeTargetDB.max();
max = max.longValue() > tmp.longValue() ? max : tmp;
}
}