我怎么知道在 RxAndroid 的 combineLatest 中哪个 observable 发生了变化?

How can I know which observable has changed in combineLatest in RxAndroid?

我正在使用 Flowable.combineLatest(query,params) 用于侦听查询更改和一些参数更改 但现在我想引入分页并监听偏移量变化,但这里的问题是当查询发生变化时我需要重置偏移量。 想知道如何使用 RxAndroid 实现它? 基本上我们观察到 3 个或更多对象(查询、偏移量、connectionChange) 我想要实现的是监听任何 observables 的变化 + 当查询改变时更新 offset

的值

也许在这里检查一下

Pagination with Rx

https://github.com/kaushikgopal/RxJava-Android-Samples#14-pagination-with-rx-using-subjects

您可以在 item 被发送到 query flowable 之后显式链接另一个调用。在注册 Flowable 之前注册 doAfterNext 可能是最简单的方法。

fun observePublishers(query: Flowable<List<String>>, connectionState: Flowable<Boolean>, offset: Flowable<Int>) {
    val newQuery = query.doAfterNext {
        index = 0
    }
    Flowable.combineLatest(newQuery, connectionState, offset) { queryResult, hasConnection, offsetValue ->
        
    }.subscribe { 
        
    }
}

要查看值的来源,您必须以某种方式标记值,这需要对每个源进行转换。例如:

data class Tuple<T>(val value: T, val index: Long) { ... }

Flowable.defer {
    var indices: Array<Long>(4) { 0 }
    var latests: Array<Long>(4) { 0 }
    Flowable.combineLatest(
        source1.map { Tuple(it, indices[0]++) },
        source2.map { Tuple(it, indices[1]++) },
        source3.map { Tuple(it, indices[2]++) },
        source4.map { Tuple(it, indices[3]++) },
        { tuple1, tuple2, tuple3, tuple4 -> 
            
             if (tuple1.index != latests[0]) {
                 // first source changed
             }
             if (tuple2.index != latests[1]) {
                 // second source changed
             }
             if (tuple3.index != latests[2]) {
                 // third source changed
             }
             if (tuple4.index != latests[3]) {
                 // fourth source changed
             }

             latests[0] = tuple1.index
             latests[1] = tuple2.index
             latests[2] = tuple3.index
             latests[3] = tuple4.index
        }
    )
}