Skip to content Skip to sidebar Skip to footer

Realm Change Listener With RealmResults Not Being Called

I'm using Realm 3.0.0 I'm fetching some objects from Realm and trying to add onChangeListener, but it does not get fired when an object is changed Here's the code, am I missing so

Solution 1:

You need to store the RealmResults as a field reference in order to ensure that Realm can update it

RealmResults<Record> realmResults;

public void etc() {
    realmResults = RealmManager.recordsDao().loadRecords();
    realmResults.addChangeListener(new RealmChangeListener<RealmResults<Record>>() {

Also, you should probably use RealmRecyclerViewAdapter from https://github.com/realm/realm-android-adapters with RealmResults<Record>, instead of ArrayList so that you actually keep a managed results in sync with your recycler view automatically


So with that in mind, all you need to do is replace your code with

public class RecordsAdapter extends RealmRecyclerViweAdapter<Record, RecordsAdapter.ViewHolder> {
     public RecordsAdapter(OrderedRealmCollection<Record> realmResults) {
         super(realmResults, true);
     }

     // ... same as before
}

and

recyclerView.setAdapter(new RecordsAdapter(RealmManager.recordsDao().loadRecords());

And just ditch your RealmChangeListener because it is incomplete and unnecessary


Post a Comment for "Realm Change Listener With RealmResults Not Being Called"