Unbinding presenters necessary in GWT

吃可爱长大的小学妹 提交于 2019-12-19 07:22:19

问题


I'm using the MVP pattern from my GWT application following the example given here http://code.google.com/webtoolkit/doc/latest/tutorial/mvp-architecture.html

I have a single MainPresenter and sub-presenter for each of the panels in the MainView. To show a new sub-presenter, I do something like this:

presenter = new PresenterA(new ViewA(), ....);
presenter.go(panel) // presenter clears the panel and itself to the panel

When PresenterA is created, it binds itself to events in the ViewA. My question is what is the proper way to switch to a new presenter? Right now, I'm simply creating a new presenter and attaching it to the same panel like this:

presenter = new PresenterB(new ViewB(), ....);
presenter.go(panel) // presenter clears the panel and itself to the panel

I have some doubts about this approach. First, am I causing a memory leak when I switch to the new presenter? I've lost both the field that references the old presenter and cleared it from the panel it was attached to. I suppose this means it should be garbage collected, but I'm not really sure. Secondly, what happens to the event bindings that the old presenter had? Will these bindings prevent the presenter from being garbage collected? Do I need unbind them first?

What is the correct way the handle the situation of switching presenters without memory leaks and binding to "dead" events.


回答1:


I'd suggest that you take a look at the gwt-mvp and/or gwt-presenter libraries, which both take the same approach to this problem. Effectively, you create a base class for all presenters which maintains an internal list of all event registrations that the presenter has. When you then come to switch presenters, you call presenter.unbind() on the old presenter, which then removes all the event handlers you've created.

The base presenter class will look something like this:

public abstract class BasePresenter {

    private List<HandlerRegistration> registrations = Lists.newLinkedList();

    public void bind() {}

    public void unbind() {
        for(HandlerRegistration registration : registrations) {
            registration.removeHandler();
        }
        registrations.clear();
    }

    protected void addHandler(HandlerRegistration registration) {
        registrations.add(registration);
    }

}

Then in the bind method of your presenter, you pass the HandlerRegistration object's into the addHandler() method:

bind() {
    addHandler(foo.addBarHandler(...));
}


来源:https://stackoverflow.com/questions/2626742/unbinding-presenters-necessary-in-gwt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!