首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何知道RxAndroid中combineLatest中的哪个可观察对象发生了更改?

如何知道RxAndroid中combineLatest中的哪个可观察对象发生了更改?
EN

Stack Overflow用户
提问于 2020-12-07 04:28:36
回答 3查看 269关注 0票数 2

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-12-11 19:31:31

要查看值来自何处,您必须以某种方式标记这些值,这需要逐个源进行转换。例如:

代码语言:javascript
运行
复制
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
        }
    )
}
票数 1
EN

Stack Overflow用户

发布于 2020-12-09 05:13:42

Stack Overflow用户

发布于 2020-12-09 05:59:18

在将item发送到query可流动对象之后,您可以显式地链接另一个调用。在注册Flowable之前注册doAfterNext可能是最简单的方法。

代码语言:javascript
运行
复制
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 { 
        
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65172763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档