Realm-java: Allow backlink fields in queries

Created on 14 Mar 2017  ·  10Comments  ·  Source: realm/realm-java

Goal

Extend @LinkingObjects feature to support queries.

Code Sample

This should work

 public class Person extends RealmObject {
   String name;
   Dog dog;
 }

 public class Dog extends RealmObject {
   String name;
   @LinkingObjects("dog")
   final RealmResults<Person> owners = null;
 }

 // Find all Dogs with at least one owner named John
 realm.where(Dog.class).equalTo("owners.dog.name", "Fido").findAll();

Tasks

  • [x] The byte code transformation must change the field accessor (cat.owners) to getting a RealmResults using Table::get_backlink_view() making something like cat.owners.where().equalTo("name", "John").findAll() possible
  • [x] Add support in RealmQuery
  • [ ] Extend query predicates in RealmQuery (equalTo() etc.) to allow querying backlinks
  • [x] Add UTs for queries
  • [ ] Documentation on link queries should be extended to describe how this can be used instead to make these kind of queries much simpler.

References

2904

Version of Realm and tooling

Realm version(s): 3.1

T-Feature

Most helpful comment

Yes, it is a bit of a "power-feature", but is required when working with a DynamicRealm where a RealmModel class might not exist, like during a migration.

Using the named field is of course much much simpler, so should be used whenever possible.

All 10 comments

We should also support the "dynamic query syntax": https://github.com/realm/realm-java/pull/2904/files#diff-b684aefe1e063faddcad9b319552f6e2R70

Use `linkingObjects(x.z).y` to search field Y on type X where X is a backlink object through Z.

@cmelchior oh, so this means that

RealmResults<Dog> dogs = realm.where(Dog.class).equalTo("linkingObjects(Person.dogs).name", "John").findAll();

in this particular scenario, Person has RealmList<Dog>, but Dog does NOT have @LinkingObjects("dogs") private final RealmResults<Person> owners but you can still query for essentially owners.dogs, except owners is unnamed so you need to specify it as linkingObjects(Person.dogs).name where the first part is essentially unnamed owners

Yes, it is a bit of a "power-feature", but is required when working with a DynamicRealm where a RealmModel class might not exist, like during a migration.

Using the named field is of course much much simpler, so should be used whenever possible.

@cmelchior the only pain part will be updating the realmfieldnameshelper :smile:

Actually, that would be fairly trivial, since it already works for RealmList. So for finding field names I just need to get the generic argument for a RealmResults instead of a RealmList 😝

@cmelchior i meant for the unnamed backlinks 👅

Yeah, RealmFieldNamesHelper is probably not going to support that, since it requires Realm model classes to even work :)

Is this in the works? This will be Realm's next killer feature

Yes, @bmeike is actively working on it.

Closing. C++ support being tracked in #4620

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mithrann picture mithrann  ·  3Comments

yuwu picture yuwu  ·  3Comments

nolanamy picture nolanamy  ·  3Comments

cmelchior picture cmelchior  ·  3Comments

Merlin1993 picture Merlin1993  ·  3Comments