Search code examples
androidobservablerx-java2multicastobservers

How to get a callback when every observer of a hot like ConnectableObservable complete?


I am searching for a doOn... callback for a ConnectableObservable that is invoked when every observer terminates

       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) }

I would like to get a callback to my multicasted hot observable when both of my observers signal a terminal event

I have tried to put doOnTerminate and doOnComplete operators on my parent multicasted observable but, it seems that these callbacks are invoked 2 times (one for each observer)


Solution

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

    Both .doOnComplete and .doOnTerminate work for me.

    Edit: the chances are you might be attaching the do... operators in incorrect order. For example, neither of these doOnComplete will work:

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