当像ConnectableObservable这样的热点的每个观察者都完成时,如何获取回调?

时间:2019-05-28 14:35:15

标签: android observable rx-java2 multicast observers

我正在搜索doOn ...回调以查找每个观察者终止时调用的ConnectableObservable

       val gatewayItems = viewModel.getGatewayItems(gateways!!)
                .observeOn(Schedulers.io())
                .take(1)
                .publish()
                .autoConnect(2)

            gatewayItems.subscribe { sharedGateways -> sharedGatewaysAdapter.submitList(sharedGateways) }

            gatewayItems.subscribe { sharedGateways -> privateGatewaysAdapter.submitList(privateGateways) }

当我的两个观察者都发出终端事件信号时,我希望获得对多播热点可观察性的回调

我尝试将doOnTerminate和doOnComplete运算符放在多播的父级上可观察到,但似乎这些回调被调用了2次(每个观察者一个)

1 个答案:

答案 0 :(得分:0)

   val gatewayItems = viewModel.getGatewayItems(gateways!!)
            .observeOn(Schedulers.io())
            .take(1)
            .doOnComplete { ... }
            .doOnTerminate { ... }
            .publish()
            .autoConnect(2)

.doOnComplete.doOnTerminate都为我工作。

编辑:您可能会以错误的顺序附加do...运算符。例如,这两个doOnComplete都不起作用:

   val gatewayItems = viewModel.getGatewayItems(gateways!!)
            .observeOn(Schedulers.io())
            .doOnComplete { ... }
            .take(1)
            .publish()
            .autoConnect(2)
            .doOnComplete { ... }