realm-java: Changed Notification raised even when the value is the same

I have a RealmObject with a private property with get / set accesor.

public class Dog extends RealmObject
{
	private String name;
	public String getName()
	{
		return name;
	}
	
	public void setName(String name)
	{
		this.name = name;
	}
}

(Nothing fancy there). I have a subscritor to a list of dogs:

RealmResults<Dog> mydogs = realm.where(Dog.class).findAll();
mydogs.addChangeListener((dogs, changeSet) -> //any update stuff);

As expected, anytime the name of any dog is changed the listener is hit, however what I didn’t expect is that the listener is hit everytime I assign a name to the name field even if it is the same as it has, I don’t know if this behavior is by design, so I had to add something like this:

       public void setName(String name)
	{
                if (!this.name.equals(name)
		     this.name = name;
	}

I know that this is an easy solution, but I wonder why the listener is launched even if the underlined data is not changed. Is this the expected behavior? (I’m using Realm 4.1.0 on a 6.0 Android emulated device)

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

+1 for a way to get notified of actual changes of the model. The update functionality is great because it saves a lot of code that is boilerplate, but if there is no way of knowing whether it modified the model, we can not use it to update views. In our case we query our server every second for a JSON of the model. I would love to just refresh the view in the on change notification but it will trigger every second regardless of changes to the JSON from the server.

See https://github.com/realm/realm-core/issues/2787 for a discussion of this on Realm Core level.

So this means that you get notification because a set operation is logged by Core which is indeed a requirement not to miss changes in a synchronized setting.

Which is why only the user knows if “not setting the value” is not necessary.

Thanks for the insight about the current status. From that thread, It seems that the decision have been made more for a technical problem, as even in the discussion is stated that it strange to receive notifications even when there is not change made. The problem with the “only the user knows” approach is that, for example, I have more than 30 classes on my model and there are a lot of properties to check and that could be cumbersome to add all the guards just to make my code to behave the “expected way” from the user point of view. Also the other approach would be to “check” when the notification is received, but again as realm has the latest changes already “loaded” on my collection I don’t have the data to compare if the field values are the same or not. Just to be clear, I’m not complaining, realm is amazing and you guys are doing and great work and this is a tricky situation and I guess one of the payloads of using sync.

Thank you for taking the time to find and post the thread discussion.