gwt return value when asynchronous callback end

孤街浪徒 提交于 2019-12-11 10:33:08

问题


Hello i have got function like that:

    @Override
    public boolean checkExist(String name) {

        final boolean check[] = new boolean[] { false };

        getAllRecordFromServer(new SearchCallback() {

            @Override
            public void onSearchResult(Map<String, Item> itemsMap) {
            //do some action set true when map key equals name  
                 check[0] = true;
            }

            @Override
            public void onSearchError(XMLPacket error) {
                // TODO Auto-generated method stub

            }
        });

        return check[0];
    }

I`am looking for solution and found some article but i do not know how to do it in gwt :/ This code do not working properly ... as you know this is asynchronous callback.

How can i fix this problem i must return value after callback ends.


回答1:


This code is not working properly ...

The reason is that Your code is in synchronous model and you are making Asynchronous calls.

I am assuming that you are doing something after you got some result in onSearchResult.

So,stop executing your code until you got the response from server, Why because you dont know that the callmay fail sometime.

If you really want to use the value globally ,then

public boolean returnVal=false;

public void checkExist(String name, MyCallback callback) {
       getAllRecordFromServer(new SearchCallback() {
            @Override
            public void onSearchResult(Map<String, Item> itemsMap) {
                  returnVal=true;
                  proceedFurther(itemsMap) //your next lines of code in this method.
            }

            @Override
            public void onSearchError(XMLPacket error) {
             stopProceedFurther(); //show  some error message to user.
            }
        });



回答2:


It is not possible to return a value from async call in a method as you have done. That is the basic nature of "Asynchronous" call. You never know when it will return ( network/server delay) and hence your code execution does not wait!!!!!

Do not return a boolean from your method. Instead make your method take a callback.

 interface MyCallback {
   execute(boolean successfl);
 }

 public void checkExist(String name, MyCallback callback) {
       getAllRecordFromServer(new SearchCallback() {
            @Override
            public void onSearchResult(Map<String, Item> itemsMap) {
            //do some action set true when map key equals name  
                 callback.execute(true);
            }

            @Override
            public void onSearchError(XMLPacket error) {
            }
        });
 }



回答3:


Maybe a cleaner solution might be to use events and an eventbus (which could be private to your class or maybe shared by everyone so that other components can react to that) when you get your result or not. Then listen for these events and treat them accordingly.

 getAllRecordFromServer(new SearchCallback() {

    @Override
    public void onSearchResult() {
        eventBus.fireEvent(new FoundEvent());
    }

    @Override
    public void onSearchError() {            
        eventBus.fireEvent(new NotFoundEvent());
    }
});


来源:https://stackoverflow.com/questions/15719866/gwt-return-value-when-asynchronous-callback-end

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