Realm java nested query on same ArrayObject

假如想象 提交于 2019-12-25 01:49:12

问题


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,

  1. email= a@a.com & type = 1
  2. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!