问题
Current code:
mRealm.where(AdditionalData.class)
.contains("checklistParticipants.email", a@a.com, Case.INSENSITIVE)
.equalTo("checklistParticipants.type", 0)
.findAll();
which returns me result of similar to ANY
record.
I want to check in nested query, only return record if and if both condition fulfilled. likewise in nested query, record email must be a@a.com and type=0
i tried below approach but ended up in same result.
mRealm.where(AdditionalData.class)
.contains("checklistParticipants.email",a@a.com, Case.INSENSITIVE)
.findAll()
.where()
.equalTo("checklistParticipants.type", 0)
.findAll();
Below screenshot shows 2 child items,
- email= a@a.com & type = 1
- email= x@x.com & type = 0
Realm checking for both value in either-or approach.
Also tried:
mRealm.where(AdditionalData.class)
.equalTo("checklistParticipants.email",a@a.com, Case.INSENSITIVE)
.and()
.equalTo("checklistParticipants.type", 0)
.findAll()
classpath "io.realm:realm-gradle-plugin:5.8.0"
UPDATE
class AdditionalData {
String name;
RealmList<ChecklistParticipants> checklistParticipants;
}
class ChecklistParticipants{
String email;
String type;
String field3;
}
回答1:
as @EpicPandaForce said you need to use LinkingObjects
class AdditionalData {
String name;
@LinkingObjects(ChecklistParticipants.rlAdditionalData)
final RealmResults<ChecklistParticipants> linkedChecklistParticipants = null;
public RealmResults<RealmDocumentLines> getLinkedChecklistParticipants() {
return linkedChecklistParticipants ;
}
}
ChecklistParticipants
class ChecklistParticipants{
String email;
String type;
String field3;
AdditionalData rlAdditionalData;
public AdditionalData getAdditionalData() {
return rlAdditionalData;
}
public void setAdditionalData(AdditionalData additionalData) {
this.rlAdditionalData = additionalData ;
}
}
Then query
RealmResult<ChecklistParticipants> result = mRealm.where(ChecklistParticipants.class)
.contains("email", a@a.com, Case.INSENSITIVE)
.equalTo("type", 0)
.findAll();
then loop over the result and use getAdditionalData()
from each item
来源:https://stackoverflow.com/questions/55758555/realm-java-nested-query-on-same-arrayobject