realm-java: RealmChangeListener onChange() method is not triggered
Goal
In Android, I am trying to update different views - such as ViewHolder
in RecyclerView
and a bunch of other custom views - that contain different instances of the logically-same object using a RealmChangeListener
.
This is the object class:
public class Post extends RealmObject {
...
}
I am currently using the FastAdapter library so I have this PostItem
class which is basically the item that is going to be stored inside the adapter and provides the logic to bind to a ViewHolder
:
public class PostItem extends AbstractItem<PostItem, PostItem.ViewHolder> {
private Post mPost;
private RealmChangeListener mRealmChangeListener;
...
public PostItem withPost(Post post) {
mPost = post;
mRealmChangeListener = new RealmChangeListener() {
@Override
public void onChange() {
// here I call a custom listener which should then update the Recycler
// of which it is responsible - something like:
// myCustomListener.onRealmChange(PostItem.this, mPost)
}
};
mPost.removeChangeListeners();
mPost.addChangeListener(mRealmChangeListener);
}
...
}
The user can then make some action such as liking or saving the post. Each time I am making a request to the server and upon success I take the object coming back in the responde and update it in the database in this way:
public class LikePostTask extends AsyncTask<Void, Void, Post> {
...
@Override
protected void onPostExecute(Post post) {
DomainController.getInstance().save(post);
}
}
public class DomainController {
...
public <T extends RealmObject> T save(final T object) {
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
T realmObject = realm.copyToRealmOrUpdate(object);
realm.commitTransaction();
return realmObject;
}
}
Expected Results
If I understand it correctly after the save()
method is executed the onChange()
method of the RealmChangeListener
should be called upon the next iteration of the run loop on the UI thread.
Actual Results
However the onChange()
method is not triggered. I tried putting a breakpoint inside it and it’s never reached.
The thing is the objects instances inside the PostItem
objects are updated. After scrolling to force a rebind of the ViewHolder
the UI is updated with the right information but again whatever I put inside the onChange()
method is never executed.
Version of Realm and tooling
Realm version(s): 0.88.2
Android Studio version: 2.1 preview 5
Which Android version and device: 6.0.1 on a Galaxy S6
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 16 (8 by maintainers)
Commits related to this issue
- Add to realmObjects when addChangeListener called * Only add the RealmObject to HandlerController.realmObjects when addChangeListener called. This will avoid we have too many objects in the map. ... — committed to realm/realm-java by beeender 8 years ago
- Add to realmObjects when addChangeListener called (#2723) * Only add the RealmObject to HandlerController.realmObjects when addChangeListener called. This will avoid we have too many objects in ... — committed to realm/realm-java by beeender 8 years ago
Thanks a lot for the project! It is a bug that
copyToRealmOrUpdate
doesn’t callHanlderController.addToRealmObjects()
(when update?) https://github.com/realm/realm-java/issues/2686#issuecomment-215298614 also needs to be considered when fix this.We will fix this ASAP. Sorry for the inconvenience.