https://kotlinlang.org logo
#rx
Title
# rx
g

gsala

02/17/2020, 8:24 AM
Hey. I have an Rx library I use to connect to a BLE device. This library has this function:
fun connect(bluetoothDevice: BluetoothDevice) : Observable<RxBleConnection>
. The reason this returns an
Observable
instead of a
Single
is that the life-cycle of the
Observable
is tied to the connection. So this observable will only ever emit one item, but it will stay alive without completing so the connection stays alive until we dispose. I want to wrap this to use coroutines instead. I'm thinking about having a function like
suspend fun connect(bluetoothDevice : BluetoothDevice) : RxBleConnection
, Any ideas on how to handle the life-cycle of the Observable? If I just use
connectObservable.awaitFirst()
it will take the first value, and dispose, which will stop the connection.
k

kioba

02/17/2020, 9:07 AM
I believe this is more a #coroutines question, maybe they can help you in that channel. I personally did not work that much with Coroutine. My suggestion would be to subscribe in a suspend function. And use global scope or channels or flow to pass the lifecycle
g

gsala

02/17/2020, 9:17 AM
I bet they'll think it's an #rx question 😛 but I'll try
k

kioba

02/17/2020, 9:19 AM
Well if they do, just message me and we can deep dive into this. Btw subscribe instead of awaitFirst will grant you the ability to control the stream from the suspend/Coroutine so that is a good start
2 Views