-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Closed
Labels
docsKDoc and API referenceKDoc and API reference
Description
The awaitClose
function currently throws a JobCancellationException
if the job was canceled before calling the function.
I would have personally expected that the block passed to awaitClose { }
gets executed without throwing this exception.
Sample code:
internal fun SensorManager.consumeValuesAsFlow(
sensor: Sensor,
measureInterval: Duration = 20L with TimeUnit.MilliSeconds
): Flow<FloatArray> {
return callbackFlow {
val sensorEventListener = object : SensorEventListener {
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) = Unit
override fun onSensorChanged(event: SensorEvent) {
catch { offer(event.values) }
}
}
registerListener(
sensorEventListener,
sensor,
measureInterval[TimeUnit.MicroSeconds].toInt(),
sensorHandler
)
try {
awaitClose { unregisterListener(sensorEventListener) }
} catch (t: Throwable) {
Log.e("SensorManager", "consumeValuesAsFlow", t) // called with JobCancellationException
}
}
}
Coroutines version 1.3.3
Kotlin version 1.3.61
Metadata
Metadata
Assignees
Labels
docsKDoc and API referenceKDoc and API reference