RealmObject changeListener [英] RealmObject changeListener

查看:167
本文介绍了RealmObject changeListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从官方文档的通知"部分了解Realm中的通知类型,并且当我在多个托管对象中使用RealmObject addChangeListener时,仅更改一个对象时,就会调用所有这些对象.

I'm trying to understand notification types in Realm from the Notifications section in the official docs, and when I'm using RealmObject addChangeListener in multiple managed object all of them are called when only one object is changing.

这是我的代码

Person first = realm.where(Person.class).equalTo("id", 0).findFirst();

first.addChangeListener(new RealmChangeListener<Person>() {
    @Override
    public void onChange(Person person) {
        Log.e(LOG_TAG, "First element is changing: " + person);
    }
});

Person second = realm.where(Person.class).equalTo("id", 1).findFirst();

second.addChangeListener(new RealmChangeListener<Person>() {
    @Override
    public void onChange(Person person) {
        Log.e(LOG_TAG, "Second person is changing: " + person);
    }
});

当我在任何一个Person对象中触发更新时(例如在第一个对象中),两个侦听器都被调用.

When I trigger an update in any of these Person objects (for example in first) both of the listeners are being called.

这是官方文档所说的:

侦听器也可以附加到RealmObject实例以及RealmResults实例.这使您可以对对象的更改和查询结果做出反应.

Listeners can also be attached to RealmObject instances as well as RealmResults instances. This allows you to react to changes to your objects and query results.

还有

最后,基于类型的更改侦听器将在其引用的类型更改时得到通知.

Lastly, typed-based change listeners will get notified when their referenced types change.

据我了解,所看到的行为与第二个定义一致,但是我需要使用第一个行为,也就是说,当与该侦听器相对应的对象发生更改时,我想得到通知. 因此,如果更新了第一个Person侦听器,则只会通知其相应的侦听器,而不是所有Person侦听器.

From what I understand the seen behaviour agrees with the second definition but I need to use the first behaviour, that's, I want to be notified when the object corresponding to that listener is changed. So, if first Person is updated, only its corresponding listener get notified, not all Person listeners.

推荐答案

现在正在发生,因为我们的更改检测不够精确.它将触发所有相同类型的对象的变更侦听器,而不仅仅是那些已更改的对象.

Right now it is happening because our change detection is not granular enough. It will trigger change listeners for all objects of the same type, not just those that changed.

在这里跟踪更改侦听器以仅通知确切对象是否已更改 https://github.com/realm/realm-java/issues/989 .

Getting the changelisteners to only notify if the exact object has changed is being tracked here https://github.com/realm/realm-java/issues/989.

这篇关于RealmObject changeListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆