android - 是否有 Handler.runWithScissors(final Runnable r, long timeout) 的 RxJava(Rxandroid) 等价物?
问题描述
是否有 Handler.runWithScissors(final Runnable r, long timeout) 的 RxJava(Rxandroid) 等价物?
我有一个问题是workerThread
异步获取长时间运行的结果。( mApplication.startApp()
)
同时通知中的进度uiThread
,我的解决方法如下:
/**
* @return Observable<String> that subscribe the progress.
*/
public Observable<String> startApp() {
Subject<String> mAppState = BehaviorSubject.create();
Observable.just("Initialize...")
.observeOn(mSchedulerProvider.single())
.doOnNext((state) -> {
mAppState.onNext(state);
mApplication.startApp();
})
.doOnError((error) -> mAppState.onError(error))
.map((state) -> "Initialization Complete.")
.doOnNext((state) -> {
TimeUnit.MILLISECONDS.sleep(1000);
mAppState.onNext(state);
mAppState.onComplete();
})
.compose(bindToLifecycle())
.subscribe();
return mAppState.compose(bindToLifecycle())
.observeOn(mSchedulerProvider.ui());
}
有人有更好的解决方案吗?谢谢。
解决方案
我不认为你的解决方案很好
Observable.just(1)
.observeOn(Schedulers.computation())
.map(integer -> {
// do your long job or computation
return integer;
})
.skip(1, TimeUnit.SECONDS)
.observeOn(AndroidScheduler.mainThread())
.map(o -> {
//UI update
return o;
}).subscribe();
推荐阅读
- firefox - Firefox Quantum ESR PKCS11 - 如何通过脚本或配置设置加载
- pentaho - 最新版本的 pentaho 解决了 pentaho 8.1 中的 oracle 错误
- c# - 修改 IList 查询语句
- java - entityManager.remove(class) 不起作用
- php - 如何在不使用 PHP MVC 中的 Extend 的情况下将数据库操作层与 BaseModel 层分离?
- c# - 变量和垃圾回收的生命周期
- c# - 当 GoogleDriveApplication 托管在 IIS 服务器中时,无法启动浏览器
- ruby - 如何将嵌套的哈希参数传递给 searchkick
- visual-studio-code - VsCode:如何避免为每个编辑器组打开多个文件?
- python - anaconda/python 3.6/MacOS 10:gdal 意外停止工作(ImportError)