0

我是 LiveData 和 Kotlin 协程的新手。我正在尝试使用 Chromium Cronet库从我的存储库类发出请求以返回 LiveData 对象。为了返回 liveData,我使用了新的 LiveData 构建器(带有 LiveData的协程)。如何从成功的 Cronet 请求中发出结果?

class CustomRepository @Inject constructor(private val context: Context, private val gson: Gson) : Repository {
    private val coroutineDispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher()

    override suspend fun getLiveData(): LiveData<List<MyItem>> = liveData(coroutineDispatcher) {
        val executor = Executors.newSingleThreadExecutor()
        val cronetEngineBuilder = CronetEngine.Builder(context)
        val cronetEngine = cronetEngineBuilder.build()
        val requestBuilder = cronetEngine.newUrlRequestBuilder(
            "http://www.exampleApi.com/example",
            CustomRequestCallback(gson),
            executor
        )
        val request: UrlRequest = requestBuilder.build()
        request.start()
    }

    class CustomRequestCallback(private val gson: Gson) : UrlRequest.Callback() {

        override fun onReadCompleted(request: UrlRequest?, info: UrlResponseInfo?, byteBuffer: ByteBuffer?) {
            byteBuffer?.flip()
            byteBuffer?.let {
                val byteArray = ByteArray(it.remaining())
                it.get(byteArray)
                String(byteArray, Charset.forName("UTF-8"))
            }.apply {
                val myItems = gson.fromJson(this, MyItem::class.java)
                // THIS IS WHAT I WANT TO EMIT
                // emit(myItems) doesn't work since I'm not in a suspending function
            }
            byteBuffer?.clear()
            request?.read(byteBuffer)
        }

        // other callbacks not shown
}

}

4

1 回答 1

1

该解决方案涉及将UrlRequest.Callback传统的回调结构包装在suspendCoroutine构建器中。

我还在一篇Medium 文章中记录了我的学习成果,该文章讨论了Cronet 与 LiveData 和 Kotlin Coroutines 的集成

override suspend fun getLiveData(): LiveData<List<MyItem>> = liveData(coroutineDispatcher) {

    lateinit var result: List<MyItem>
    suspendCoroutine<List<MyItem>> { continuation ->

        val requestBuilder = cronetEngine.newUrlRequestBuilder(
            "http://www.exampleApi.com/example",
            object : UrlRequest.Callback() {

                // other callbacks not shown

                override fun onReadCompleted(request: UrlRequest?, info: UrlResponseInfo?, byteBuffer: ByteBuffer?) {
                    byteBuffer?.flip()
                    byteBuffer?.let {
                        val byteArray = ByteArray(it.remaining())
                        it.get(byteArray)
                        String(byteArray, Charset.forName("UTF-8"))
                    }.apply {
                        val myItems = gson.fromJson(this, MyItem::class.java)
                        result = myItems
                        continuation.resume(result)
                    }
                    byteBuffer?.clear()
                    request?.read(byteBuffer)
                },
                executor
        )
        val request: UrlRequest = requestBuilder.build()
        request.start()

    }
    emit(result)
}
于 2019-11-16T22:42:22.173 回答