2016-04-04 73 views
0

所有呼叫和回叫都可以正常工作。由於http錯誤代碼,我只是遇到了一個問題,我在回調中克隆並重試了一次調用(使用相同的回調)。克隆呼叫會導致主線程回撥被呼叫

public static abstract class MyCallback<T> implements Callback<T> { 
    @Override 
    public void onResponse(Call<T> call, retrofit2.Response<T> response) { 
     Timber.d("is this the main thread %b", Looper.myLooper() == Looper.getMainLooper()); 
     if (response.isSuccessful()) { 
      //success handling 
     } else { 
      if (response.code() == 406) { 
       // remedy reason for failure 
       call.clone().enqueue(MyCallback.this); 
      } 
     } 
    } 

    @Override 
    public void onFailure(Call<T> call, Throwable t) { 
     Timber.e("onFailure %s", t.getMessage()); 
    } 
} 

這給:

is this the main thread true 
is this the main thread false 
is this the main thread false 
is this the main thread false 
is this the main thread false 
[looping] 

我與其他的/新的回調發揮各地,開始一個新的呼叫,等等。只有當我克隆呼叫的調用回調函數關閉主線程。 出現的實際問題是,在成功重試呼叫後,我無法更改UI。

使用改造2.0.1

+0

你絕對相信,它不是在主線程上運行?你有沒有嘗試改變用戶界面,並得到一個異常? – muratgu

+0

@muratgu是的。我只是偶然發現它,因爲我得到了「視圖只能從它們創建的線程編輯」異常。只有這樣我纔有。那些looper檢查日誌.. – Till

+1

我想你在改造或okhttp中發現了一個bug。我只是確認,如果在入隊之前通過克隆方法創建調用,則回調上的執行線程不是主線程。 – muratgu

回答