Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gwt 引用异步回调的响应错误实践?_Gwt - Fatal编程技术网

Gwt 引用异步回调的响应错误实践?

Gwt 引用异步回调的响应错误实践?,gwt,Gwt,今天,我在GWT代码中看到了一个方法,如下所示: public Map< String, ResponseObject > getResponse( RequestObject requestObject ) { remoteServiceAsync = GWT.create( RemoteServce.class ); final Map< String, ResponseObject > responseReference = new HashMap&

今天,我在GWT代码中看到了一个方法,如下所示:

public Map< String, ResponseObject > getResponse( RequestObject requestObject ) {
    remoteServiceAsync = GWT.create( RemoteServce.class );
    final Map< String, ResponseObject > responseReference = new HashMap< String, ResponseObject >();

    remoteServiceAsync.doServerCall( requestObject, new AsyncCallback< ResponseObject >() {
      public void onFailure(Throwable caught) {
         // TODO: Do something with errors.
      }

      public void onSuccess( ResponseObject response ) {
        responseReference.put( "value", response );
      }
    } );

    return( responseReference );
};
publicmapgetResponse(RequestObject-RequestObject){
remoteServiceAsync=GWT.create(RemoteServce.class);
最终映射responseReference=newhashmap();
remoteServiceAsync.doServerCall(requestObject,新的AsyncCallback(){
失败时的公共无效(可丢弃){
//TODO:处理错误。
}
成功时公共无效(响应对象响应){
responseReference.put(“值”,响应);
}
} );
返回(响应引用);
};

我认为,这是一种非常糟糕的做法。你觉得这个代码怎么样?它是否适用于所有浏览器,它是否是读取
数据
的“
有效
”选项?我必须提到,数据是在程序中后期访问的,因此并不缺少数据。

返回
Hashmap
的做法非常糟糕。它完全破坏了
RPC的优势

在该方法中,它立即返回一个
空哈希映射
,然后返回插入该
映射
的值,该值没有任何用处

还有初始化

甚至没有检查以下行的
(remoteServiceAsync!=null)

remoteServiceAsync = GWT.create( RemoteServce.class );

因此,它将为每个调用初始化。

这里有2点要突出显示-

1) 我们不知道异步调用何时返回,因此,我们可以使用return语句返回从RPC返回的对象。因此,responseReference将始终为空

2) 我们不需要每次需要启动异步调用时都创建RPC实例。因此,对于这种类型的场景,建议使用单例模式

您的代码段可以重构如下-

ServiceAsync serviceasync = null;

getServiceAsync()
{
     if(serviceAsync == null)
     {
         serviceAsync = GWT.create( RemoteServce.class );
     }
     return serviceAsync;
}

AsynCallBack asyncCallBack;

getAsyncCallBack()
{
      if(asynvCallback != null)
      {
           return asyncCallback;
      }
      return new AsyncCallback< ResponseObject >() 
            {
                  public void onFailure(Throwable caught) 
                  {
                       // TODO: Do something with errors.
                  }

                   public void onSuccess( ResponseObject response ) 
                   {
                        // Do processing here with the response object ...
                   }
            } );    
}
因此,将只有一个服务实例和一个异步回调实例。避免创建多个对象,从而提高性能

getServiceAsync().doServerCall( requestObject, getAsyncCallBack() );