title: Realm (Android/Kotlin) - How to perform a query which allows you to test if all objects match as a part of values in an array of values
body: <p>Context :</p>
  
  <ol>
  <li>Some content (Sheet, Category, etc.) are protected by groups. (Content class has 2 RealmLists of groups, the first "groupsOneOf" an the second "groupsAllOf")</li>
  <li>Users can be in groups (User has one RealmList "groups")</li>
  <li>Some content is available only for user which is <em>at least in one of the content's group</em>.</li>
  <li>Some content is available only for user which is <em>in all of content's group</em>.</li>
  </ol>
  
  <p>For <em>"at least in one of the content's group"</em>, I found 'in' operator in documentation :</p>
  
  <blockquote>
    <p>This allows you to test if objects match any value in an array of values.</p>
  </blockquote>
  
  <p>But for the second, no way to perform this kind of query. I searched solution in other posts but some people seems to say that's not supported right now. In iOS, it seems to be available with NSPredicate but not in java.</p>
  
  <p>Here is my code :</p>
  
  <pre><code>fun &lt;E: RealmObject&gt; RealmQuery&lt;E&gt;.groupsAllowed(): RealmQuery&lt;E&gt;{
      val userGroupsIds = realm.queryUserConnected()?.findFirst()?.groups?.toGroupIds()
      if(userGroupsIds == null || userGroupsIds.isEmpty()){
          isEmpty("groupsAllOf")
          isEmpty("groupsOneOf")
      }else{
          beginGroup()
              beginGroup()
                  isEmpty("groupsOneOf")
                  or()
                  `in`("groupsOneOf.id", userGroupsIds.toTypedArray())
              endGroup()
              //beginGroup()
                  //isEmpty("groupsAllOf")
                  //or()
                  //TODO
              //endGroup()
          endGroup()
      }
      return this
  }
  </code></pre>
  
  <p>Currently I'm performing a post filter when I get the RealmResults for groupsAllOf : </p>
  
  <pre><code>fun &lt;E: RealmObject&gt; E.groupsAllowed(userGroupIds: List&lt;String&gt;): E?{
      val groupsAllOf: RealmList&lt;Group&gt;? = when(this){
          is Sheet -&gt; groupsAllOf
          is Category -&gt; groupsAllOf
          else -&gt; null
      }
      if(groupsAllOf == null || groupsAllOf.isEmpty()){
          return this
      }else{
          return if(userGroupIds.containsAll(groupsAllOf.toGroupIds())) this else null
      }
  }
  
  fun &lt;E: RealmObject&gt; RealmResults&lt;E&gt;.groupsAllowed(): List&lt;E&gt;{
      val userGroupIds = realm.queryUserConnected().findFirst()?.groups?.toGroupIds()?: emptyList()
      val listAllowed = arrayListOf&lt;E&gt;()
      this.forEach { it.groupsAllowed(userGroupIds)?.let { contentAllowed -&gt;  listAllowed.add(contentAllowed) } }
      return listAllowed
  }
  
  fun RealmList&lt;Group&gt;.toGroupIds(): List&lt;String&gt; = arrayListOf&lt;String&gt;().apply { this@toGroupIds.forEach { group -&gt; this.add(group.id) } }
  </code></pre>
  
  <p>But this is annoying because I have to no forget to call this function when I get the RealmResults :/</p>
  
  <p>Some help would be greatly appreciated.</p>