简体   繁体   English

使用 collectInto 从 RxKotlin/RxJava 中的两个可观察源构建列表

[英]Building a list from two observable sources in RxKotlin/RxJava using collectInto

I have a Category data class and a Plan data class.我有一个Category数据类和一个Plan数据类。 Each Category has a list of plan ids.每个Category都有一个计划 ID 列表。 There are Categories and Plans stored via Room.有通过 Room 存储的类别和计划。 I am trying to construct a local List<Any> where I add each category to a list, and then add each of it's plans.我正在尝试构建一个本地List<Any> ,我将每个类别添加到列表中,然后添加它的每个计划。

So for each category, add the category to the list, then add each plan that belongs to that category.因此,对于每个类别,将类别添加到列表中,然后添加属于该类别的每个计划。

The final result would look something like this...最终的结果看起来像这样......

0 -> a Category
1 -> a Plan
2 -> a Plan
3 -> a Plan
4 -> a Category
5 -> a Plan

etc.等等。

The following calls sucessfully return an Observable<List<Category>> and an Observable<Plan>以下调用成功返回一个Observable<List<Category>>和一个Observable<Plan>

AppDatabase
   .getDatabase(context)
   .categoryDao()
   .getAll()

AppDatabase.getDatabase(context).planDao().getPlan(planId)

Here I am trying to build my list, but it actually never emits when I subscribe to it.在这里,我试图建立我的列表,但当我订阅它时它实际上从未发出。 No completion, or error.没有完成,或错误。 Everything else in the stream get's hit.流中的其他所有内容都会受到影响。 Why can't I get my final result?为什么我不能得到我的最终结果?

    fun fetchCategoriesAndPlans() {
    val items = mutableListOf<Any>()
    AppDatabase
        .getDatabase(context)
        .categoryDao()
        .getAll()
        .concatMap { listOfCategories ->
            listOfCategories.toObservable()
        }
        .doOnNext { category ->
            items.add(category)
        }
        .concatMap { category ->
            category.getPlanIds()!!.toObservable()
        }
        .flatMap { planId ->
            AppDatabase.getDatabase(context).planDao().getPlan(planId)
        }.collectInto(items, BiConsumer{ list, i ->
            Log.d(TAG, "Collect into")
            list.add(i)
        })
        .subscribeBy(
            onSuccess = {
                Log.d(TAG, "Got the list")
            },
            onError = {
                Log.e(TAG, "Couldn't build list ${it.message}", it)
            })
}

I make a demo base on your case which help emit both Category and Plan我根据您的案例制作了一个演示,这有助于发布CategoryPlan

override fun onCreate(savedInstanceState: Bundle?) {
    ...

    getCategories()
        .flattenAsObservable { it }
        .flatMap { getPlanWithCategory(it) }
        .toList()
        .subscribe({
            for (item in it) {
                Log.i("TAG", " " + item.javaClass.canonicalName)
            }
        }, {

        })
}

fun getPlanWithCategory(category: Category): Observable<Any> {
    val getPlansObservable = Observable.fromArray(category.planIds).flatMapIterable {
        it
    }.flatMap {
        getPlan(it).toObservable()
    }
    return Observable.concat(Observable.just(category), getPlansObservable)
}


fun getPlan(planId: String): Single<Plan> {
    return Single.just(Plan())
}

fun getCategories(): Single<List<Category>> {
    val categories = arrayListOf<Category>()
    categories.add(Category(arrayListOf("1", "2", "3")))
    categories.add(Category(arrayListOf("1", "2")))
    return Single.just(categories)
}

class Category(val planIds: List<String>)

class Plan

Out put输出

 I/TAG:  Category
 I/TAG:  Plan
 I/TAG:  Plan
 I/TAG:  Category
 I/TAG:  Plan
 I/TAG:  Plan

Hope it help希望有帮助

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

相关问题 在RxJava中使用一个作为谓词组成两个可观察的源 - Composing two observable sources using one as a predicate in RxJava RxJava-两个Observable源,仅在某些值上组合输出 - RxJava - two Observable sources, combine output only on certain values 使用RxKotlin时如何指定RxJava版本? - How to specify the version of RxJava when using RxKotlin? RXKOTLIN/RXJAVA:使用 Observables 的套接字之间的通信 - RXKOTLIN/RXJAVA: Communication between the socket using Observables RxJava-依次运行两个可观察对象,使用第一个可观察对象中的结果 - RxJava - Run two observables sequentially, using result from first observable in second observable 从Rxjava中的Observable获取列表的最佳方法 - Best way to get List from Observable in Rxjava 在RxJava中从2个可观察项列表中创建一个可过滤的可观察项 - Create a filtered observable from 2 observable of list of items in RxJava RxJava - Observable 的 zip 列表 - RxJava - zip list of Observable 使用LiveData,RxJava / RxKotlin和Spek在Android上进行的测试中的不稳定 - Flakiness in tests on Android using LiveData, RxJava/RxKotlin and Spek 使用 RxJava、Retrofit、RxKotlin flatmap 依次调用多个 API - Calling multiple API in sequence using RxJava,Retrofit,RxKotlin flatmap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM