Realm Change Listener with RealmResults not being called

前端 未结 1 1867
无人及你
无人及你 2020-12-18 10:00

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

相关标签:
1条回答
  • 2020-12-18 10:18

    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

    0 讨论(0)
提交回复
热议问题