Existing 3-function callback to Kotlin Coroutines

前端 未结 2 1614
眼角桃花
眼角桃花 2021-01-30 02:19

I have a general question with a specific example: I\'d like to use Kotlin coroutine magic instead of callback hell in Android when taking a picture.

manager.op         


        
2条回答
  •  半阙折子戏
    2021-01-30 02:42

    In this particular case you can use a general approach to convert a callback-based API to a suspending function via suspendCoroutine function:

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

    Now, in your application code you can just do manager.openCamera(cameraId) and get a reference to CameraDevice if it was opened successfully or null if it was not.

提交回复
热议问题