GWT - MVP事件总线。创建多个处理程序 [英] GWT - MVP Event bus. Multiple handlers getting created

查看:118
本文介绍了GWT - MVP事件总线。创建多个处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



每当用户导航到我们的客户编辑页面时,一个新的演示者已创建,并设置了一个视图。有一个主要的客户编辑和主视图。主视图中还有子视图,由主要演示者的子演示者使用。在子演讲者中,我在事件总线上注册事件处理程序。

我遇到的问题是,当第二次导航到联系人编辑器时,会再次创建演示者,然后再次注册这些事件。现在,当事件发生时,它将被处理两次,每个演示者实例一次。

演示者不会被变量保留,但子视图会被主视图引用。难道这个视图中的引用是保持事件处理程序不被删除?我的印象是,如果事件处理程序被垃圾收集,它将被删除。如果情况并非如此,我应该从事件总线上取消注册这些事件处理程序吗?

更新:
演示者不会被垃圾收集。
如果我可以修改代码以删除不再需要的所有对这些演示者的引用,它们是否会被收集,因此将删除事件处理程序?

解决方案

事件处理程序是独立的对象实例,它们是针对EventBus内的某种集合进行设置和存储的。您的演示者只是创建它们并将它们传递给EventBus,所以处理程序仍然在参与者的整个生命周期中被引用(这就是它们继续运行的原因)。演示者可能没有被垃圾收集,因为处理程序可能仍然引用他们或他们的字段。



创建新的演示者后,每次检测到时都会再次添加处理程序,所以解决方案是在实例化新的演示者之前清除现有的处理程序,或者跟踪它们,并在添加新的处理程序时不添加新的处理程序。



我采取的方法是重新使用屏幕级别的View / Presenter实例,并在重新输入时重置其状态。这也有助于提高性能。演示者还会跟踪它们添加到EventBus的所有处理程序的HandlerRegistration实例。



我不知道Colin提到的ResettableEventBus,但这听起来像是一件好事解决方案。

I am working in a large application I inherited, and am running into some best practices issues.

Every time a user navigates to our Customer Edit page, a new presenter is created, and a view is set. There is a main presenter for the customer edit, and a main view. There are also sub views within the main view that are used by sub presenters of the main presenter. Within the sub presenters I register event handlers on the event bus.

The issue I'm having is that when a navigates to the contact editor a second time, the presenter is created again, and those events are registered again. Now when an event occurs, it is handled twice, once for each presenter instance.

The presenter isn't held onto by a variable, but the sub view is referenced by the main view. Could It be that this reference in the view is keeping the event handlers from being removed? I was under the impression that event handlers will be removed if that object was garbage collected. If this is not the case, should I be un-registering these event handlers from the event bus?

Update: The presenters are not being garbage collected. If I can modify the code to remove all references to those presenters when they are no longer needed, will they be collected, and therefore will the event handlers be removed?

解决方案

The event handlers are separate object instances that are set and stored against some kind of collection inside the EventBus. Your presenter just creates them and passes them to the EventBus, so the handlers are still referenced beyond the lifetime of the presenter (and that's why they keep running). The presenters are probably not being garbage collected because the handlers may still reference them or their fields.

Having a new presenter created adds the handlers again every time as you've detected, so the solution is to either clear the existing handlers prior to instantiating the new presenter or to keep track of them and not add new handlers when they're already added.

The approach I take is to reuse the screen-level View/Presenter instances and reset their state when re-entering. This also helps with performance. The presenters also keep track of the HandlerRegistration instances for all handlers they add to the EventBus.

I wasn't aware of ResettableEventBus that Colin mentions, but that sounds like a good solution as well.

这篇关于GWT - MVP事件总线。创建多个处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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