• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

List does not contain objects added in anonymous inner class.

 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I am having an anonymous inner class in which I am adding objects to a list which is declared outside the inner class. After the control comes out of the inner class the list size is again zero. Can anyone explain why is this so?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you checked that your onSuccess method is actually being called ?
If it is, have you checked that the list that is passed to it actually contains any objects ?
Your onFailure method is empty ? How do you know if an exception is being thrown or not ?
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:Have you checked that your onSuccess method is actually being called ?
If it is, have you checked that the list that is passed to it actually contains any objects ?
Your onFailure method is empty ? How do you know if an exception is being thrown or not ?
I used print statements, success method is being called and the list contents are printed in my console.
 
Rob Spoor
Sheriff
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a GWT asynchronous callback. The "asynchronous" part here is key - it means that the code in the onSuccess / onFailure methods will be executed at some unspecified time in the future.

What happens with that code is as follows:
1) The contacts List is created.
2) An HTTP call is made to the listContacts method of the rpcService.
3a) The contacts list is printed.
4a) The contacts list is returned.

3b) The rpcService call returns a List<Contact>.
4b) The contacts list is filled with the contents of this List.

The problem is, you don't know when steps 3b and 4b occur. They could occur almost immediately, or they could occur a minute later if the network latency is high.

So unfortunately, your design will not work the way you want it. That's something you will run into not just with GWT but with all frameworks that use asynchronous callbacks (like ActionListeners in AWT / Swing).


I had to deal with the same issue, and I solved it by adding another callback. This callback was provided by the code that called the method, and the asynchronous method simply calls the callback method.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:That's a GWT asynchronous callback. The "asynchronous" part here is key -
Oops!!! I missed it. I am sorry for being a stupid, the name itself says its an asynchronous call. I could not notice it. Thank you Rob.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic