RxJava
Describe
- subscribeOn() 指定上游发送事件的线程, observeOn 指定的是下游接收事件的线程
- 多次指定上游的线程只有第一次指定的有效,就是subscribeOn()只有第一次有效,其余忽略
- 可以多次指定下游线程,每调用一次observeOn(),下游的线程就会切换一次
Demo在 AndroidDemo -> RXLeran->RX基础->Rx线程切换
链式调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| RetrofitFactory.create(ICommonApis.class) .imgUpServer(part) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .doOnNext(new Consumer<PictureUp>() { @Override public void accept(PictureUp pictureUp) throws Exception { List<PictureUp.ModelListBean> picList = pictureUp.getModel_list(); if (picList != null && !picList.isEmpty()) { String feet_id = picList.get(0).getFile_group_id(); if (!isEmpty(feet_id) && mvpView != null) { mvpView.upFeetId(feet_id); } } } }) .observeOn(Schedulers.io()) .flatMap(new Function<PictureUp, ObservableSource<BaseCount>>() { @Override public ObservableSource<BaseCount> apply(PictureUp pictureUp) throws Exception { List<PictureUp.ModelListBean> picList = pictureUp.getModel_list(); if (picList != null && !picList.isEmpty()) { String feet_id = picList.get(0).getFile_group_id(); if (!isEmpty(feet_id) && mvpView != null) { mvpView.upFeetId(feet_id); }else { return Observable.error(new Exception()); } } return RetrofitFactory.create(ICommonApis.class).offlineBoxInfo(requestBody); } }) .subscribe(new Consumer<BaseCount>() { @Override public void accept(BaseCount baseCount) throws Exception { if (mvpView == null) { return; } if (baseCount.getMeta().getStatusCode() == AUTH_FAILED) { mvpView.unOfficeEquip(baseCount); } else if (baseCount.getMeta().getStatusCode() == 0) { mvpView.upSuccess(); } else { if (baseCount.getMeta() != null && !isEmpty(baseCount.getMeta().getDescribe())) { ToastUtil.showBiggerText(baseCount.getMeta().getDescribe()); } } } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (mvpView != null) { mvpView.upUserInfoFailed(); } } });
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| fun getImgScan() { val request = RetrofitWeChatFactory.create(IApiStore::class.java) request.get_access_token() .flatMap { request.getTicket(it.access_token, 2) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(object : Observer<WeChatSecond> { override fun onComplete() { }
override fun onSubscribe(d: Disposable) { }
override fun onNext(t: WeChatSecond) { val noncestr = LoginNewActivity.getRandomString(8) val timeStamp = (System.currentTimeMillis() / 1000).toString() t.ticket?.let { val string1 = String.format( "appid=%s&noncestr=%s&sdk_ticket=%s×tamp=%s", WeChatAppID, noncestr, it, timeStamp ) val sha = EncryptUtils.getSHA(string1)
mvpView.sign(noncestr, timeStamp, sha) } }
override fun onError(e: Throwable) { }
}) }
|
http://yamlee.me/2020/03/11/2020-03-11-%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3RxJava%E4%B8%8ERxKotlin/
http://reactivex.io/documentation/operators.html
https://www.jianshu.com/p/fa1828d70192
https://www.cnblogs.com/fuyaozhishang/p/8697404.html
Sample
水管系列
https://github.com/ReactiveX/RxAndroid
https://github.com/amitshekhariitbhu/RxJava2-Android-Samples
https://github.com/rengwuxian/RxJavaSample
https://github.com/ssseasonnn/RxJava2Demo
https://juejin.im/post/5b8f536c5188255c352d3528
RxJava 只看这一篇文章就够了(上)
RxJava 只看这一篇文章就够了 (中)
RxJava 只看这一篇文章就够了 (下)
使用MVP Dagger2 https://juejin.im/post/5d5ce44d5188252231108e68
https://juejin.im/user/590210f4ac502e0063d338f5/posts