Budius Budius - 4 months ago 19
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

RealmList
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
id
.

So a typical query on it looks like:

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


This creates a slightly overhead of
listening/subscribing
to data because now I need to check
listUser.isLoaded()
use
ListUser
to
addChangeListener/removeChangeListener
and
ListUser.list
as an actual data on my adapter.

So my question is:

Is there a way I can query this realm to receive a
RealmResults<Photo>
. That way I could easily use this data in
RealmRecyclerViewAdapter
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).

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

Answer

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>() {
    @Override
    public void onChange(RealmModel element) {
        RealmResults<Photo> photos = listPhoto.getList().where().findAll();
        // do stuff with your photo results here.


        // unregister the listener.
        listPhoto.removeChangeListeners();
    }
});

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).

Comments