小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

android – 在我更新到Retrofit 2.0之后,在不同的線程中發(fā)布了對onNext的onNext調(diào)用

 印度阿三17 2019-06-28

在我們使用Retrofit 1.9時,我的同事創(chuàng)建了以下課程

public class SomeApiCallAction {

  private Subscription subscription;
  private NoInternetConnectionInterface noInternetConnectionInterface;

  public interface NoInternetConnectionInterface {
      PublishSubject<Integer> noInternetConnection(Throwable throwable);
  }

  public void execute(Subscriber subscriber, NoInternetConnectionInterface noInternetConnectionInterface) {
      this.noInternetConnectionInterface = noInternetConnectionInterface;
      this.subscription = retrofit.someService().someApiCall()
          .subscribeOn(Schedulers.newThread())
          .observeOn(AndroidSchedulers.mainThread())
          .subscribe(subscriber)
          .retryWhen(retryFunction);
  }

  public void cancel() {
      if (this.subscription != null) {
          this.subscription.unsubscribe();
      }
  }             

  private Func1<Observable<? extends Throwable>, Observable<?>> retryFunction = new Func1<Observable<? extends Throwable>, Observable<?>>() {
      @Override
      public Observable<?> call(Observable<? extends Throwable> observable) {
          return observable.flatMap(new Func1<Throwable, Observable<?>>() {
              @Override
              public Observable<?> call(final Throwable throwable) {
                  if (noInternetConnectionInterface!= null && (throwable instanceof IOException || throwable instanceof SocketTimeoutException)) {
                      return noInternetConnectionInterface.noInternetConnection(throwable);
                  }else{
                      return Observable.error(throwable);
                  }
              }
          });
      }
}

SomeApiCallAction只是一個簡單的類,它包含了內(nèi)部的改進api調(diào)用,唯一特別的是它的重試功能.重試函數(shù)將檢查throwable是否是IOException或SocketTimeoutException,如果是,它將調(diào)用接口,以便我們可以向用戶提供重試對話框,詢問他們是否要重試該操作.我們的用法類似于以下代碼段

public class SomeActivity implement NoInternetConnectionInterface {

    @OnClick(R.id.button)
    public void do(View v) {
        new SomeApiCallAction().execute(
            new Subscriber(),
            this
        )
    }

    @Override
    public PublishSubject<Integer> noInternetConnection(final Throwable throwable) {
        Log.i("Dev", Thread.currentThread()   " Error!");
        final PublishSubject<Integer> subject = PublishSubject.create();

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                NoInternetDialogFragment dialog = NoInternetDialogFragment.newInstance();
                dialog.setNoInternetDialogFragmentListener(new NoInternetDialogFragmentListener{
                    @Override
                    public void onUserChoice(boolean retry, NoInternetDialogFragment dialog) {
                        Log.i("Dev", Thread.currentThread()   " Button Click!");
                        if (retry) {
                            subject.onNext(1);
                        } else {
                            subject.onError(throwable);
                        }

                        dialog.dismiss();

                    }
                });
                dialog.show(getSupportFragmentManager(), NoInternetDialogFragment.TAG);
            }
        });
        return subject;
    }
}

當我們使用Retrofit 1.9.0時,這個實現(xiàn)工作得非常完美.我們通過打開飛行模式進行測試,然后按下按鈕執(zhí)行api呼叫.

>第一次執(zhí)行失敗,我在重試功能中得到了UnknownHostException.
>所以,我調(diào)用界面(Activity)來顯示重試對話框
>我仍然在飛行模式下按重試按鈕重復執(zhí)行
>正如預期的那樣,用戶按下重試按鈕后發(fā)生的每次執(zhí)行都失敗了,我總是在重試功能中得到UnknownHostException.
>如果我一直按下重試按鈕,重試對話框?qū)⒂肋h顯示,直到我關閉飛行模式.

但在我們更新我們的依賴關系之后

'com.squareup.retrofit2:retrofit:2.0.2'
'com.squareup.retrofit2:adapter-rxjava:2.0.2'

我們再試一次,但這次行為改變了,

>第一次執(zhí)行失敗,我在重試功能中獲得了與之前相同的UnknownHostException.
>所以,我調(diào)用界面(Activity)來顯示重試對話框
>我仍然在飛行模式下按重試按鈕重復執(zhí)行
>但是這一次,在重試函數(shù)中,我得到了NetworkOnMainThreadException而不是像它那樣接收UnknowHostException.
>因此條件不匹配,接口沒有被調(diào)用,結果只有1個重試對話框呈現(xiàn)給用戶.

以下是上面代碼的日志

Thread[android_0,5,main] Error!
Thread[main,5,main] Button Click!

你知道這會導致什么嗎?任何建議,評論都會非常感激.

注意:以下是我們一直在使用并可能相關的其他依賴項.但是它們最近沒有更新,從本項目開始就使用這些版本.

'com.jakewharton:butterknife:8.0.1'

'io.reactivex:rxandroid:1.1.0'
'io.reactivex:rxjava:1.1.0'

'com.google.dagger:dagger-compiler:2.0'
'com.google.dagger:dagger:2.0'
'javax.annotation:jsr250-api:1.0'

更多信息

我只是將我的代碼重置回到我們使用Retrofit 1.9的時候,我發(fā)現(xiàn)打印日志不同

Thread[Retrofit-Idle,5,main] Error!
Thread[main,5,main] Button Click!

不確定這是否與問題有關,但很明顯,在1.9.0中,我將不同線程中的接口調(diào)用為2.0.0

最終編輯

在閱讀了@JohnWowUs的答案并按照他提供的鏈接后,我發(fā)現(xiàn)在Retrofit 2中,網(wǎng)絡調(diào)用默認是同步的

要解決我的問題,有兩種方法可以解決此問題

1.)按照@JohnWowUs的建議,通過為retryFunction指定線程

this.subscription = retrofit.someService().someApiCall()
  .subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(subscriber)
  .retryWhen(retryFunction, Schedulers.io());

2.)創(chuàng)建改造對象時,在創(chuàng)建RxJavaCallAdapterFactory時指定線程

retrofit = new Retrofit.Builder()
  .baseUrl(AppConfig.BASE_URL)
  .client(client)
  .addConverterFactory(GsonConverterFactory.create(getGson()))
  .addCallAdapterFactory(
     RxJavaCallAdapterFactory.createWithScheduler(
       Schedulers.from(threadExecutor)
     )
   )
  .build();

解決方法:

我認為問題在于,當您重新訂閱時,由于在retryWhen中使用默認的trampoline調(diào)度程序而在主線程上訂閱. Retrofit 1.9為您處理了調(diào)度,因此使用subscribeOn毫無意義.問題討論是here.在Retrofit 2中我相信這已經(jīng)改變了所以你應該嘗試類似的東西

this.subscription = retrofit.someService().someApiCall()
      .subscribeOn(Schedulers.io())
      .observeOn(AndroidSchedulers.mainThread())
      .subscribe(subscriber)
      .retryWhen(retryFunction, Schedulers.io());
來源:https://www./content-4-275151.html

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多