简体   繁体   English

在这种情况下如何在 MainThread 上运行 doOnSubscribe

[英]How run doOnSubscribe on MainThread in this case

I want to execute doOnSubscribe block on the main thread.我想在主线程上执行 doOnSubscribe 块。 I'm trying this:我正在尝试这个:

fun test(){
    Single
        .fromCallable {
            print(Thread.currentThread())
            getCachedProfile()
        }
        .observeOn(AndroidSchedulers.mainThread())
        .flatMap {
            print(Thread.currentThread())
            api
                .getProfile()
                .doOnSubscribe {
                    print(Thread.currentThread())
                }
                .observeOn(Schedulers.io())
                .map {
                    print(Thread.currentThread())
                    it.profile
                }
                .map { it.id }
                .flatMap { photoId ->
                    print(Thread.currentThread())
                    api
                        .getPhotos(photoId)
                        .map { it.toDomain() }
                }
        }.subscribeOn(Schedulers.io())
        .observeOn(AndroidSchdulers.mainThread())

}

doOnSubscribe run on the main thread, but .getProfile() run on UI thread too and I get an error NetworkOnMainThreadException. doOnSubscribe 在主线程上运行,但 .getProfile() 也在 UI 线程上运行,我收到错误 NetworkOnMainThreadException。 How set the schedulers so doOnSubscribe is executed on the main thread and getProfile() run on the IO thread如何设置调度器,所以 doOnSubscribe 在主线程上执行,getProfile() 在 IO 线程上运行

Add .subscribeOn(Schedulers.io) to your .getProfile() call so, .getProfile().subscribeOn(Schedulers.io).observeOn(AndroidSchedulers.mainThread().doOnSubscribe().subscribeOn(Schedulers.io)添加到您的.getProfile()调用中, .getProfile().subscribeOn(Schedulers.io).observeOn(AndroidSchedulers.mainThread().doOnSubscribe()

This is how it's done in java, I imagine Kotlin has the same.这就是它在 Java 中的实现方式,我想 Kotlin 也是如此。

.doOnSubscribe{} is calling something to do before the task is executed .doOnSubscribe{}在任务执行之前调用了一些要做的事情

.subscribeOn() tells what thread to do the work on .subscribeOn()告诉在哪个线程上进行工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Observer.onNext内部运行IO操作(在mainThread内部调用) - How to run IO operation inside Observer.onNext that's called inside mainThread Android:如何在MainThread中等待AsyncTask完成? - Android: how to wait AsyncTask to finish in MainThread? RxJava 如何在旧版本的 mainThread 上观察? 我无权访问 AndroidSchedulers.mainThread() - RxJava how to observe on mainThread in older versions? I dont have access to AndroidSchedulers.mainThread() Android:如何在当前主线程中使用mFusedLocationClient回调(结果位置) - Android : How to use mFusedLocationClient callback (resulting location) in current mainthread 在IO线程上调用doOnSubscribe - doOnSubscribe is called on IO thread PublishSubject doOnSubscribe未调用 - PublishSubject doOnSubscribe not called 如何使AndroidScheduler.mainThread与Scala Observable一起使用? - How to make AndroidScheduler.mainThread work with a Scala Observable? 如何为使用 AndroidSchedulers.mainThread() 的函数编写测试; 来自 RxAndroid - How to write a test for a function that uses AndroidSchedulers.mainThread(); from RxAndroid MainThread处于“阻止”状态时如何显示ANR对话框 - How ANR dialogs shows while MainThread is in Blocked state 本地服务如何在MainThread中运行而不影响UI操作 - how local service is running in MainThread without affecting UI operations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM