Unrelated facilitators needed in the GWT

I am using the MVP template from my GWT application, following the example here http://code.google.com/webtoolkit/doc/latest/tutorial/mvp-architecture.html

I have one MainPresenter and a sub-presenter for each of the panels in MainView. To show a new sub-presenter, I am doing 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 to events in ViewA . The question is, how to switch to the new leader? Right now, I'm just creating a new presenter and attaching it to the same panel as 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, do I cause a memory leak when switching to a new master? I lost the field that refers to the old presenter and cleared it of the panel to which it was attached. I suppose that means garbage collection, but I'm not sure. Secondly, what happens to the event bindings that the old presenter had? Will these bindings prevent the lead from being garbage collected? Do I need to untie them first?

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

+6
java javascript memory-leaks mvp gwt
source share
1 answer

I would advise you to take a look at gwt-mvp and / or gwt-presenter , which use the same approach to this problem. In fact, you are creating a base class for all speakers, which maintains an internal list of all event logs that the host has. When you come to switch from presenters, you call presenter.unbind() on the old presenter, which then deletes all the event handlers that you 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 binding method of your presenter, you pass the HandlerRegistration object to the addHandler() method:

 bind() { addHandler(foo.addBarHandler(...)); } 
+6
source share

All Articles