I'm trying to use GreenRobot to share RealmResults between fragments. When the user clicks certain buttons changing the data I want to show, I call invalidateRealmResult:
public void invalidateRealmResult() {
RealmQuery<Climb> realmQuery = mRealm.where(Climb.class);
// other query modifiers added here....
mResult = realmQuery.findAllSorted("date", Sort.ASCENDING);
RealmChangeListener listener = new RealmChangeListener<RealmResults<Climb>>() {
@Override
public void onChange(RealmResults<Climb> element) {
Log.d(TAG, "Realmresult onchange");
EventBus.getDefault().postSticky(new RealmResultsEvent(mResult));
}
};
mResult.addChangeListener(listener);
EventBus.getDefault().postSticky(new RealmResultsEvent(mResult));
}
My greenrobot event looks like this:
public class RealmResultsEvent {
public RealmResult mResult;
public RealmResultsEvent(RealmResult result){
this.mResult = result
}
}
Any fragment that needs to update with the new result subscribes to the event:
@Subscribe(sticky = true)
public void onRealmResultEvent(RealmResultsEvent event) {
mResult = event.mResult;
updateView();
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
I've made sure that all fragments subscribe in onStart() and unsubscribe in onStop().
My issue is that new event posts don't appear to overwrite the old RealmResults
. I can tell that the RealmResults
is not being destroyed because the log message I have in the RealmChangeListener
gets called an increasing amount whenever I add to the realm database, and every once in a while the UI shows the wrong query results.
05-13 10:36:55.523 17684-17684/com.example.grant.wearableclimbtracker D/MainActivity: Realmresult onchange
05-13 10:36:55.536 17684-17684/com.example.grant.wearableclimbtracker D/MainActivity: Realmresult onchange
05-13 10:36:55.544 17684-17684/com.example.grant.wearableclimbtracker D/MainActivity: Realmresult onchange
05-13 10:36:55.551 17684-17684/com.example.grant.wearableclimbtracker D/MainActivity: Realmresult onchange
05-13 10:36:55.558 17684-17684/com.example.grant.wearableclimbtracker D/MainActivity: Realmresult onchange
05-13 10:36:55.566 17684-17684/com.example.grant.wearableclimbtracker D/MainActivity: Realmresult onchange
05-13 10:36:55.574 17684-17684/com.example.grant.wearableclimbtracker D/MainActivity: Realmresult onchange
05-13 10:36:55.583 17684-17684/com.example.grant.wearableclimbtracker D/MainActivity: Realmresult onchange
05-13 10:36:55.591 17684-17684/com.example.grant.wearableclimbtracker D/MainActivity: Realmresult onchange
05-13 10:36:55.600 17684-17684/com.example.grant.wearableclimbtracker D/MainActivity: Realmresult onchange
05-13 10:36:55.608 17684-17684/com.example.grant.wearableclimbtracker D/MainActivity: Realmresult onchange
05-13 10:36:55.616 17684-17684/com.example.grant.wearableclimbtracker D/MainActivity: Realmresult onchange
Every time I make a change, it calls the listener an alternating 3, 6, 12, 6, 3... times, repeating that pattern. Not sure what that means or if that information helps anyone.