我有一个关于具体例子的一般问题:我想在拍照时使用 Kotlin 协程魔术而不是 Android 中的回调地狱。
manager.openCamera(cameraId, object : CameraDevice.StateCallback() {
override fun onOpened(openedCameraDevice: CameraDevice) {
println("Camera onOpened")
// even more callbacks with openedCameraDevice.createCaptureRequest()....
}
override fun onDisconnected(cameraDevice: CameraDevice) {
println("Camera onDisconnected")
cameraDevice.close()
}
...
我怎样才能把它转换成不那么丑陋的东西? 是否可以采用具有三个左右函数的平均回调,并通过将主流指定为承诺-结果路径将其转换为承诺链?如果是这样,我是否应该/是否使用协程使其异步?
我喜欢带有异步和 .await 的东西,这将导致
manager.open(cameraId).await().createCaptureRequest()
我正在尝试通过以下内容来做到这一点,但我认为我没有正确使用CompletableDeferred
!
suspend fun CameraManager.open(cameraId:String): CameraDevice {
val response = CompletableDeferred<CameraDevice>()
this.openCamera(cameraId, object : CameraDevice.StateCallback() {
override fun onOpened(cameraDevice: CameraDevice) {
println("camera onOpened $cameraDevice")
response.complete(cameraDevice)
}
override fun onDisconnected(cameraDevice: CameraDevice) {
response.completeExceptionally(Exception("Camera onDisconnected $cameraDevice"))
cameraDevice.close()
}
override fun onError(cameraDevice: CameraDevice, error: Int) {
response.completeExceptionally(Exception("Camera onError $cameraDevice $error"))
cameraDevice.close()
}
}, Handler())
return response.await()
}
在这种特殊情况下,您可以使用通用方法通过suspendCoroutine
函数将基于回调的 API 转换为挂起函数:
suspend fun CameraManager.openCamera(cameraId: String): CameraDevice? =
suspendCoroutine { cont ->
val callback = object : CameraDevice.StateCallback() {
override fun onOpened(camera: CameraDevice) {
cont.resume(camera)
}
override fun onDisconnected(camera: CameraDevice) {
cont.resume(null)
}
override fun onError(camera: CameraDevice, error: Int) {
// assuming that we don't care about the error in this example
cont.resume(null)
}
}
openCamera(cameraId, callback, null)
}
现在,在您的应用程序代码中,您只需执行manager.openCamera(cameraId)
并获取对CameraDevice
是否成功打开或null
(如果未成功打开(的引用。
使用挂起可取消协程而不是挂起协程进行适当的异常处理
suspend fun CameraManager.openCamera(cameraId: String): CameraDevice? =
suspendCancellableCoroutine { cont ->
val callback = object : CameraDevice.StateCallback() {
override fun onOpened(camera: CameraDevice) {
cont.resume(camera)
}
override fun onDisconnected(camera: CameraDevice) {
cont.resume(null)
}
override fun onError(camera: CameraDevice, error: Int) {
// Resume the coroutine by throwing an exception or resume with null
cont.resumeWithException(/* Insert a custom exception */)
}
}
openCamera(cameraId, callback, null)
}
最好始终选择挂起可取消协程来处理协程范围的取消,或从底层 API 传播取消。其他很好的例子的来源
我已经为这类事情使用了 2 种解决方案。
1:将接口包装在扩展中
CameraDevice.openCamera(cameraId: Integer,
onOpenedCallback: (CameraDevice) -> (),
onDisconnectedCallback: (CameraDevice) ->()) {
manager.openCamera(cameraId, object : CameraDevice.StateCallback() {
override fun onOpened(openedCameraDevice: CameraDevice) {
onOpenedCallback(openedCameraDevice)
}
override fun onDisconnected(cameraDevice: CameraDevice) {
onDisconnectedCallback(cameraDevice)
}
})
}
2:制作一个具有更多功能接口的简单容器类:
class StateCallbackWrapper(val onOpened: (CameraDevice) -> (), val onClosed: (CameraDevice) ->()): CameraDevice.StateCallback() {
override fun onOpened(openedCameraDevice: CameraDevice) {
onOpened(openedCameraDevice)
}
override fun onDisconnected(cameraDevice: CameraDevice) {
onClosed(cameraDevice)
}
}
就个人而言,我会从这样的东西开始,然后在此基础上构建任何线程差异。