0

Almost same question than Realm, complex linked query, but with a important variant :

In the official docs, https://realm.io/docs/java/latest/#link-queries - there's an example how to select owners of "Brown" dogs and owner of "Fluffy" dogs. Not, as written in former question, "Brown Fluffy" dogs, because the code examples do not realize a conjunction.

So my question is: how to get only persons who own a "Brown" and "Fluffy" dog (both conditions on the same dog)? That is only U2, given the data of the example. The second code example from the docs adds a third filter "Yellow", so the answer seems right, but there is still not a conjunction on the fields of the same Dog. Below are my attempts from the docs:

// returns both U1 and U2, because U1 owns a Brown dog, and U2 a Fluffy
RealmResults<Person> r1 = realm.where(Person.class)
            .equalTo("dogs.name", "Fluffy")
            .equalTo("dogs.color", "Brown")
            .findAll();
// returns both U1 and U2, because U1 has a Fluffy but it is red, and has also a brown dog (Fido)
RealmResults<Person> r2 = realm.where(Person.class)
            .equalTo("dogs.name", "Fluffy")
            .findAll()
            .where()
            .equalTo("dogs.color", "Brown")
            .findAll();

I would need to check a conjunction of conditions over subelements in array. I need to realize it as a Query, to fill an adapter. It is possible with Realm?

I was dreaming of a new kind of grouping for conditions which allow to specify several conditions on items of a List field, like this:

RealmResults<Person> r3 = realm.where(Person.class)
    .beginFilterAny("dogs") // keep only Persons whose at least one dog satisfy:
        .equalTo("name", "Fluffy")
        .equalTo("color", "Brown")
    .endFilterAny()
    .findAll();
TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
Pierre N.
  • 11
  • 3

2 Answers2

0

You can define an inverse relationship and query for the dog by name and color. You can then iterate over the dogs and get the owners.

geisshirt
  • 2,457
  • 1
  • 17
  • 20
  • The point is not to get the owners, but to build automatically a managed collection for a RealmRecyclerViewAdapter. If I build a RealmList by a iteration of the dogs, it won't be managed. But I may be able to use [link](https://stackoverflow.com/a/40632613/5585820) to filter the results. – Pierre N. Dec 16 '18 at 08:42
0

Use a query on Dog.class with those conditions, so you'll have all Brown and Fluffy dogs. Iterate the result to extract all dogs primary key and use it in a query on Person.class to retrieve all persons owning at least one of those dogs.

//get all brown and fluffy dogs
RealmResults<Dog> dogs = realm.where(Dog.class)
     .equalTo("name", "Fluffy")
     .equalTo("color", "Brown")
     .findAll();

//extract dogs id
Set<UUID> dogIds = dogs.stream()
    .map(d -> d.getId().toString())
    .collect(Collectors.toSet());

//find all brown and fluffy dogs owners
RealmResults<Person> persons = realm.where(Person.class)
     .in("dogs.id", dogIds.toArray(new String[dogIds.size()]))
     .findAll();
Maelig
  • 2,046
  • 4
  • 24
  • 49