Lukasz Kalnik
01/25/2023, 2:04 PMsuspendCancellableCoroutine
if a callback causes calling continuation.resume()
twice?
E.g.
suspendCancellableCoroutine { continuation ->
bleManager.startScan(
onDeviceAppeared = { device ->
continuation.resume(Unit.right())
}
},
onScanError = {
continuation.resume(it.left())
},
)
}
If I get a second onDeviceAppeared
callback will the coroutine try to resume again?streetsofboston
01/25/2023, 2:05 PMLukasz Kalnik
01/25/2023, 2:05 PMstreetsofboston
01/25/2023, 2:06 PMLukasz Kalnik
01/25/2023, 2:08 PMif (!resumed)
or something like this?streetsofboston
01/25/2023, 2:09 PMLukasz Kalnik
01/25/2023, 2:10 PMif (device.name == myName)
check therestreetsofboston
01/25/2023, 2:11 PMdevicesFlow.first()
on it to get the first one.Lukasz Kalnik
01/25/2023, 2:11 PMstreetsofboston
01/25/2023, 2:11 PMLukasz Kalnik
01/25/2023, 2:13 PMstreetsofboston
01/25/2023, 2:13 PMLukasz Kalnik
01/25/2023, 2:13 PMresume
cannot be called twicestreetsofboston
01/25/2023, 2:14 PMemit
can be called many times.Lukasz Kalnik
01/25/2023, 2:16 PMsuspendCancellableCoroutine
I filter for the specific device inside of the callback. That means, if onDeviceAppeared
will be called twice for the same device, I would try to resume
twice.
2. In case of a callbackFlow
I would just emit the same device twice -> no problem, as the collection on consumer side probably already finished anyway (because we stopped collecting when we got the desired device).callbackFlow
is even easier to implement here.streetsofboston
01/25/2023, 2:17 PM.filter
operator.Lukasz Kalnik
01/25/2023, 2:18 PMstreetsofboston
01/25/2023, 2:18 PM