Lawrence Macharia Lawrence Macharia - 2 months ago 49
Android Question

Android Realm Update Asynchronously Using RxJava

I have this query to update data already in my realm table;

for (MyGameEntrySquad squad : response.body().getSquad()) {
subscription = realm.where(RealmPlayer.class).equalTo("id", squad.getPlayer().getId())
.findFirstAsync()
.asObservable()
.subscribe(new Action1<RealmObject>() {
@Override
public void call(RealmObject realmObject) {

}
});

}


I would like to perform this query asynchronously then display the results on the UI.

Basically, whatever is been returned by response.body().getSquad() has an id matching a record already in the DB; and that is what am using in my equalTo method.

Based on the data received, I would like to update two columns on each of the record matching the IDs.

However, I am facing a few challenges on this:


  1. The Action1 in subscribe is returning a RealmObject instead of a PlayerObject

  2. How to proceed from here



Any guidance on this will be appreciated.

Thanks

Answer
realm.where(RealmPlayer.class).equalTo("id", squad.getPlayer().getId())
                        .findFirstAsync()
                        .<RealmPlayer>asObservable()
                        .subscribe(new Action1<RealmPlayer>() {
                            @Override
                            public void call(RealmPlayer player) {

                            }
                        });

You should do like that.

Btw, it's bad idea to do it in a cycle - check in method of RealmQuery.