RxJava and Retrofit2: NetworkOnMainThreadException

前端 未结 2 1983
后悔当初
后悔当初 2020-12-06 09:49

I realize that I am using subscribeOn()/observeOn() on the MainThread. What are the set of options I can pass into subscribeOn()? What are the set of options I can pass into

2条回答
  •  醉梦人生
    2020-12-06 10:36

    You should call Observable.unsubscribeOn(Schedulers.io()), retrofit will unsubscribe at the end of a http-request.

    In RxJavaCallAdapterFactory of retrofit-rxjava-adapter

    it actions like this.

    if (!subscriber.isUnsubscribed()) {
        subscriber.onCompleted();
    }
    

    But when subscriber is a SafeSubscriber, it'll call unsubscribe finally.

    I have this issue in my app.

    Full code:

    o.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io());
    

提交回复
热议问题