realm / realm-java

Realm is a mobile database: a replacement for SQLite & ORMs
http://realm.io
Apache License 2.0
11.47k stars 1.75k forks source link

Observe link field changes in one listener #4445

Open idok595 opened 7 years ago

idok595 commented 7 years ago

change OrderedRealmCollectionChangeListener to support notification about specific items changes, for now the OrderedRealmCollectionChangeListener has ability to notify only about that some items has changes, and is not specificy what parameters in each item change, maybe it will be better to provide also infromation about what parameters change in each item?

beeender commented 7 years ago

@idok595 I am sorry but I don't quite get your idea. Is #4366 what you want?

idok595 commented 7 years ago

was a mistake, i mean OrderedRealmCollectionChangeListener not RealmObjectChangeListener

Zhuinden commented 7 years ago

I assume something like how if there is any object that has changed, then for those objects also provide a List<ObjectChangeSet>

beeender commented 7 years ago

Wouldn't register a RealmObjectChangeListener on the relevant RealmObject solve the problem? And IMO separated listeners fit RecycleView + ViewHodler better. OrderedRealmCollectionChangeListener to update RecycleView and RealmObjectChangeListener to update the individual view holder.

cmelchior commented 7 years ago

Note that it is partly performance related that we don't provide object change information for collections. Imagine you had a list of 1000+ tweets and marked them all as read. That would result in a massive overhead.

That said we want our change listeners to be as useful as possible, so we definitely want feedback such as this, but could your provide more information about your exact use case?

Zhuinden commented 7 years ago

If the RealmResults listeners are called before the RealmObject listeners, then this should work just fine if the view holder keeps the field reference to its actual RealmObject on a given index, and a listener (field in viewholder) is added to the realmObject when it is bound (and removed from the previously bound item)

idok595 commented 7 years ago

yes, you see in recyclerViewAdapter there is build in functionality to update specific things using RecyclerView.Adapter.notifyItemRangeChanged(int positionStart, int itemCount, Object payload), in order to performe that and not rebind the all obejct, i need to get notification about the specific change, yes it is possible to mange it and create wrapper to OrderedRealmCollectionChangeListener + ObjectChangeListener, but in terms of performance better to do it on the core side...

beeender commented 7 years ago

Parse all child links change will be a lot of overhead in the Realm side, consider below object

public class GrandChild {
    RealmList<GrandChild> grandChildren;
}
public class Parent {
    String name;
    RealmList<Child> children;
}

For above model, we need to check if any relevant Child changes and as well as the any GrandChild changes. Depends on the size of the list, it could be painfully slow.

However, maybe something like:

collection.addChangeListener(listener, linkFieldToObserve = {"children", "children.grandChilden"})

would help in this case? So it will avoid unnecessary overhead and still give user ability to observe link fields changes in a unique listener.