目前正在找工作,有看上我技艺的,深圳地区请联系我。
listenzz / live Goto Github PK
View Code? Open in Web Editor NEWA RxJava Transformer handle Android Lifecycle as same as LiveData.
A RxJava Transformer handle Android Lifecycle as same as LiveData.
如题这个怎么处理。
如题。
这个代码需要的最低版本为什么会这么高,去到26了
我写的以下代码, x 时间后执行 xx 操作
Observable.timer(2000, TimeUnit.MILLISECONDS)
.compose(Live.bindLifecycle(this))
.subscribe({
LogUtil.d("ok")
//....
}, {
LogUtil.e(it)
})
看起来貌似没有问题,但是执行后,没有任何输出,onNext
和onError
都没有被回调,关键是没有任何报错.
看了下源码,跟踪了一下,原因: Live.java#67 assertMainThread();
判断当前线程是否为主线程,不是就报错.而我上述的代码,onNext 执行是不在主线程上的,所以这里抛异常了,但是!onError
没有收到这个异常信息!!
解决办法: 在compose(Live.bindLifecycle(this))
前加上.observeOn(AndroidSchedulers.mainThread())
望优化异常处理.
在第一个页面使用,
Observable.create(new ObservableOnSubscribe() {
@OverRide
public void subscribe(ObservableEmitter e) throws Exception {
Thread.sleep(3000);
e.onNext("a");
e.onComplete();
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.compose(Live.bindLifecycle(this))
.subscribe(it -> Log.d(TAG, it));
然后调用第二个页面,然后等待发送数据,之后无法接收到上游发送的数据,原因是调用了onComplete()方法,导致considerNotify方法里面的!mDisposable.isDisposed()为false
验证code:
val d = Observable.create<Int> {
for (i in 0..1000) {
Thread.sleep(500)
LogUtil.d("it.isDisposed=" + it.isDisposed + ",i=" + i)
if (it.isDisposed) {
break
}
it.onNext(i)
}
it.onComplete()
}.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.compose(Live.bindLifecycle(this))
.subscribe({
LogUtil.d(it)
}, { it.printStackTrace() })
bu.postDelayed({
d.dispose()
}, 2000)
2s 之后调用 dispose(),不加 .compose(Live.bindLifecycle(this))
时,效果正常,2s 后会停掉输出,加了之后,onNext()虽然没有被调用,但是 it.isDisposed=false,i=
一直在输出.
我本来是在做下载文件显示进度的功能,但是取消下载一直不生效,取消之后,文件还是在后台下载完了....
调试了好久,才发现是这里的问题.
正常情况我觉得应该是加 Live.bindLifecycle()
之后和没加之前的"表现"应该一直才对.
谢谢.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.