Android Realm ChangeListener not being triggered

前端 未结 2 1578
野趣味
野趣味 2021-01-12 17:31

I\'ve got a Realm results change listener that isn\'t being triggered, here\'s the code:

final RealmResults entries = realm.where(LogEntry.cl         


        
相关标签:
2条回答
  • 2021-01-12 17:56

    If you want to use the realmChangeListener, then make your RealmResult<E> variable a Global variable. Only then it will get triggered on database change.

    0 讨论(0)
  • 2021-01-12 18:10

    You need to keep a class reference to entries to prevent it from being GC'ed:

    public MyClass {
    
      private RealmResults<LogEntry> entries;
    
      public void myMethod() {
    
        entries = realm.where(LogEntry.class).findAll();
        entries.addChangeListener(new RealmChangeListener<RealmResults<LogEntry>>() {
                @Override
                public void onChange(RealmResults<LogEntry> results) {
                    Log.v("Testing", "The size is: " + results.size());
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题