Wednesday, June 30, 2010

Contacts2 example's bug

If anyone encountered some weird behavior trying to download and run
the Contacts2 example from (http://code.google.com/webtoolkit/articles/
mvp-architecture-2.html
) and is having the following symptoms, I have
the solutions for u:

1.) adding new contact sometimes causes other contacts to disappear.
2.) having multiple ajax calls to
ContactsServiceImpl.updateContact(...) from just one single click to
the save button.

For (1), this is a classic problem using a collection's size as the id
for new items. As the contacts map changes in size due to add/delete
operation, new contacts might incorrectly take on existing contact's
id. (Try delete first contact on the list before u add one). Solution
is to simply put a synchronized id generation method in the
ContactsServiceImpl and use that to get the id for new contact instead
of size. Simple enough.

For (2), this is the fun part and upon digging a bit in the code
through the debugger, the problem comes from the fact that the views
are cached inside AppController. Take a look at onValueChange(...) and
u will see they are cached. Problem is, the presenter's constructor is
calling bind() and that adds a clickHandler everytime. So depends on
how many times u went to the add/edit page and hit, hitting save
causes the list of clickHandler to execute the ajax call sequentially.
This can be observed by putting a breakpoint on
ContactsServiceImpl.updateContact(...) method. U can see threads
piling up there for no good reason. I was scratching my head and
thinking my browser has some funky plugin, but clearing them still
have the problems and only digging the code a bit deeper reveals the
problem.

The simplest solution is to remove the caching of the views and u will
only get one ajax call for every button click.

On top of the two problems, there are something that still puzzle me:
a.) the add & edit is taking different path towards completing the
goal, which resulted in the ContactsServiceImpl.addContact(..) method
never called.... Take a look at AppController.doAddNewContact() and
doEditContact() and u will see.

b.) I still don't understand why ContactsViewImpl<ContactDetails> is
templated while EditContactView is a direct concrete class. Some
expert here might be able to shed some light here.

But at the end, what puzzles me is why Google putting a confusing
example out there for people to learn their already complex MVP-2
model? It just add that much frustration and confusion on top of
everything. And be honest, with all the genius there in Google, I was
expecting something more complete and consistent.

Hope this helps and wish u all encounter less problems then I do while
learning,
Joseph

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment