我遇到以下代码导致NetworkOnMainThreadException的问题。
InternetConnectionUtil.isInternetOn()
.flatMap { isInternetOn -> if (isInternetOn) RxFirebaseDatabase.observeValueEvent(vinylRef,{it.children.map { it.value as String }}) else Flowable.error(Exception("No internet connection")) }
.flatMap {stylesList -> Flowable.fromIterable(stylesList) }
.flatMap { style -> vinylRepository.getVinyls(style)}
.toObservable()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer)
但是当我用以下方式模拟样式列表时:
InternetConnectionUtil.isInternetOn()
.flatMap { isInternetOn -> if (isInternetOn) Flowable.fromIterable(listOf("House")) else Flowable.error(Exception("No internet connection")) }
.flatMap { style -> vinylRepository.getVinyls(style)}
.toObservable()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer)
我得到了预期的回应......有什么想法吗?