Budius Budius - 1 year ago 83
Android Question

Query realm data contained on other object

This question is a follow-up question from: Organize Android Realm data in lists

Due to the data returned by the API we use, it's slightly impossible to do an actual query on the realm database. Instead I'm wrapping my ordered data in a

and adding a
@PrimaryKey public String id;
to it.

So our realm data looks like:

public class ListPhoto extends RealmObject {
@PrimaryKey public String id;
public RealmList<Photo> list; // Photo contains String/int/boolean

which makes easy to write to and read from the Realm DB by simply using the API endpoint as the

So a typical query on it looks like:

realm.where(ListPhoto.class).equalTo("id", id).findFirstAsync();

This creates a slightly overhead of
to data because now I need to check
as an actual data on my adapter.

So my question is:

Is there a way I can query this realm to receive a
. That way I could easily use this data in
and use listeners directly on it.

Edit: to further clarify, I would like something like the following (I know this doesn't compile, it's just a pseudo-code on what I would like to achieve).

.equalTo("id", id)
.findFirstAsync() // get a results of that photo list
.findAllAsync(); // get the field "list" into a `RealmResults<Photo>`

Answer Source

First you query the ListPhoto, because it's async you have to register a listener for the results. Then in that listener you can query the result to get a RealmResult.

Something like this

final ListPhoto listPhoto = realm.where(ListPhoto.class).equalTo("id", id).findFirstAsync();
listPhoto.addChangeListener(new RealmChangeListener<RealmModel>() {
    public void onChange(RealmModel element) {
        RealmResults<Photo> photos = listPhoto.getList().where().findAll();
        // do stuff with your photo results here.

        // unregister the listener.

Note that you can actually query a RealmList. That's why we can call listPhoto.getList().where(). The where() just means "return all".

I cannot test it because I don't have your code. You may need to cast the element with ((ListPhoto) element).