realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

How to get only the relations matching the where clause when using link queries?

Open cornflakes opened this issue 11 years ago • 2 comments

Say I have two classes:

Foo extends ReamlObject {
    RealmList<Bar> bars;
}

Bar extends RealmObject {
    boolean active;
}

When I then make the query:

RealmResults<Foo> fooResults = realm.where(Foo.class).equalTo("bars.active", true).findAll();

It returns all Foos that have atleast one active Bar, but in the RealmList: bars all nonactive Bar object still resides.

How can I make a link query where this is not the case?

cornflakes avatar Dec 21 '14 09:12 cornflakes

If I understand you correctly, you want to filter the bars list so it only contains those elements that match a certain query?

That is currently not possible, at least not as a one-liner. Instead you would have to do something like this:

RealmResults<Foo> fooResults = realm.where(Foo.class).equalTo("bars.active", true).findAll();
for (Foo foo : fooResults) {
  RealmResult<Bar> activeBars = foo.getBars().where().equalTo("active", true);
  // ...
}

I hope it makes sense?

cmelchior avatar Dec 21 '14 10:12 cmelchior

@cornflakes I assume your question was answered, please reopen if that was not the case.

cmelchior avatar Jan 05 '15 10:01 cmelchior