Realm-java: Link queries question

Created on 21 Oct 2016  路  8Comments  路  Source: realm/realm-java

Recently i have studied link queries example with Person and Dogs:
https://realm.io/docs/java/latest/#link-queries

I am wondering how should I construct query in order to find Person which has dog named "Fluffy" and that dog has "brown" color at the same time. The obvious result of such query is Person U2 named John, however I have no idea how I should construct such query. Any help?

T-Help

Most helpful comment

Try this:

// r2 => [U2]
RealmResults<Person> r2 = realm.where(Person.class)
                             .equalTo("dogs.name", "Fluffy")
                             .findAll() // This the key point. Build a query based on the first query's result.
                             .where()
                             .equalTo("dogs.color", "Brown")
                             .findAll();

All 8 comments

Try this:

// r2 => [U2]
RealmResults<Person> r2 = realm.where(Person.class)
                             .equalTo("dogs.name", "Fluffy")
                             .findAll() // This the key point. Build a query based on the first query's result.
                             .where()
                             .equalTo("dogs.color", "Brown")
                             .findAll();

Thanks a lot! However I have some doubts. According to description from the example the result of your query should be U1, U2.

The query behind r2 is different. Let鈥檚 begin by breaking this query apart. The first portion of the query looks like this: RealmResults r2a = realm.where(Person.class).equalTo("dogs.name", "Fluffy").findAll();. It matches U1 and U2. Then, r2b = r2a.where().equalTo("dogs.color", "Brown").findAll(); also matches U1 and U2 (both persons have brown dogs).

Didn't check it yet, but there must be mistake somewhere - either in documentation or suggested query is incorrect.

@beeender out of curiousity, would this work?

// r2 => [U2]
RealmResults<Person> r2 = realm.where(Person.class)
                             .equalTo("dogs.name", "Fluffy")
                             .findAllAsync() // This the key point. Build a query based on the first query's result.
                             .where()
                             .equalTo("dogs.color", "Brown")
                             .findAllAsync();

?

Could you shorten it to:

RealmResults<Person> r2 = realm.where(Person.class)
                             .equalTo("dogs.name", "Fluffy")
                             .equalTo("dogs.color", "Brown")
                             .findAll();

or would that give wrong results?

@astigsen that will return has at least one dog with the name Fluffy and has at least one dog with the color Brown

I thought you wanted has at least one dog with the name Fluffy which has color brown?

Thanks, I had missed that the queries involved lists rather than individual links.

The documentation is correct, and queries above will result in [U1, U2]. You really need #607 to express it more elegant. So far, you can manually add and maintain relationship in the opposite direction i.e., a field Person to Dog. Then you can query for the dog and follow the link back to the owner/person.

@Zhuinden Thanks a lot for the use case in https://github.com/realm/realm-java/issues/3668#issuecomment-255378795 . It won't work now since the first findAllAsync() won't give a valid TableView immediately and it causes the 2nd where crash. I add an item in https://github.com/realm/realm-java/issues/3372

Since the original question has been answered, I will just close this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Frasprite picture Frasprite  路  3Comments

mithrann picture mithrann  路  3Comments

bryanspano picture bryanspano  路  3Comments

Merlin1993 picture Merlin1993  路  3Comments

CNyezi picture CNyezi  路  3Comments