2016-04-04 74 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

回答