数据准备就绪时,通过改造提醒主线程

时间:2017-12-18 09:40:36

标签: android multithreading retrofit

当我在onResponse中获取数据时,我有一个改装请求, 我在textviews中做了多次插入,我在上面的代码中调用了大量工作,如果有的话,我从OnReponse得到结果,否则我从数据库得到结果,所以问题我在OnResponse和OnFailure中有相同的代码,所以有什么办法把我的繁重工作放在改造之外,并等待响应从OnResponse或OnFailure得到一个结果?

bool CheckBuyOrder1(double buy1_tp){
for( int i = 0 ; i < OrdersTotal() - 1 ; i++ ) {
  OrderSelect( i, SELECT_BY_POS, MODE_TRADES );
  if( OrderTakeProfit() == buy1_tp && OrderComment() == "buy" ) 
return(true);
}
return(false);
}

bool CheckSellOrder1(double sell1_tp){
for( int i = 0 ; i < OrdersTotal() - 1 ; i++ ) {
  OrderSelect( i, SELECT_BY_POS, MODE_TRADES );
  if( OrderTakeProfit() == sell1_tp && OrderComment() == "sell" ) 
return(true);
}
return(false);
}

int totalOrders = OrdersTotal();

void OnTick()
{
if(totalOrders != OrdersTotal()){
double vbid    = MarketInfo("EURUSD",MODE_BID);
double bid = NormalizeDouble(vbid, 3);
double market_buy_tp = bid;
double buy1= bid + 0.002;
double buy1_tp= bid + 0.003;
if(CheckOpenOrders1(market_buy_tp)==false && CheckBuyOrder1(buy1_tp)==false){
int ticket9=OrderSend(Symbol(),OP_BUYSTOP,Lots,buy1,MaxSlippage,0,buy1_tp,"buy",16380,0,clrGreen);
}

double market_sell_tp = bid;
double sell1 = bid - 0.003;
double sell1_tp= bid - 0.004;
if(CheckOpenOrdersSell1(market_sell_tp)==false && CheckSellOrder1(sell1_tp)==false ){
int ticket19=OrderSend(Symbol(),OP_SELLSTOP,Lots,sell1,MaxSlippage,0,sell1_tp,"sell",16380,0,clrGreen);
}
totalOrders = OrdersTotal();
}}

3 个答案:

答案 0 :(得分:0)

您可以像这样更改

//create a interface
    public interface ConfirmationCallback {
          void onSuccess(YourResponseClass value);
          void onError();
    }

    //call this method from your class
    yourApiCall(new ConfirmationCallback() {
                @Override
                public void onSuccess(YourResponseClass value) {
                    realm.beginTransaction();
                    dashboard = realm.copyToRealmOrUpdate(value);
                    realm.commitTransaction();
                    // heavy work : insert in data in multiple text views
                }

                @Override
                public void onError() {
                    dashboard = realm.where(Dashboard.class).findFirst();
                    // heavy work : insert in data in multiple text views
                }
            });


         public void yourApiCall(final ConfirmationCallback confirmationCallback){
            call.enqueue(new Callback<Dashboard>() {
                @Override
                public void onResponse(Call<Dashboard> call, Response<Dashboard> response) {
                   confirmationCallback.onSuccess(response.body());
                }

                @Override
                public void onFailure(Call<Dashboard> call, Throwable t) {
                    Log.e("error ", "" + t.getMessage());
                    confirmationCallback.onError();
                }
            }
        }

答案 1 :(得分:0)

试试这个.. 首先创建一个接口..让我们称之为OKCallback。

public interface OKCallback {
void onSuccess(String result);

void onFailure(String result);

}

然后在启动改造请求的方法中,像这样传递最终的OKCallback okCallback ..

 public void NetworkCall(final OKCallback okCallback){

...........
call.enqueue(new Callback<Dashboard>() {
            @Override
            public void onResponse(Call<Dashboard> call, Response<Dashboard> response) {

                realm.beginTransaction();
                dashboard = realm.copyToRealmOrUpdate(response.body());
                realm.commitTransaction();

               // heavy work : insert in data in multiple text views
               okCallback.onSuccess(parcel);

}

最后简单地(ActivityX在任何类或活动中实现OKCallback)你应该能够在那里做繁重的工作。你也可以使用Handler将你的数据包装在onSuccess方法中。如图所示。

 @Override
public void onSuccess(String result) {
    Handler handler = new Handler(ActivityX.this.getMainLooper());

    //process result and
    handler.post(new Runnable() {
        @Override
        public void run() {

            //heavy work done here will run on UI thread
        }
    });
}

答案 2 :(得分:0)

你可以创建一个接口并在主线程上回调,或者在onSuccess()或onfailure()中获得api调用的响应后,启动一个新的AsynTask并在后台处理请求。