Bil*_*ean 5 asynchronous kotlin okhttp
我正在用 Kotlin 制作一个应用程序。到目前为止,我的网络调用不必一起使用。我现在需要进行两个并发网络调用,暂停直到我收到他们的两个响应,然后继续执行。我正在尝试完成这样的事情:
//first networking call, get resourceOne
var resourceOne : String?
Server.asyncRequest(RequestBuilder(endpoints.second, ids, params)) { resource: String?, error: ServiceError? ->
resourceOne = resource
}
//second networking call, get resourceTwo
var resourceTwo : String?
Server.asyncRequest(RequestBuilder(endpoints.third, ids, params)) { resource: String?, error: ServiceError? ->
resourceTwo = resource
}
//do something here wiith resourceOne and resourceTwo
Run Code Online (Sandbox Code Playgroud)
我的 asyncRequest 函数的函数头是:
fun asyncRequest(requestBuilder: RequestBuilder, completion: (resource: String?, error: ServiceError?) -> Unit) {
Run Code Online (Sandbox Code Playgroud)
它只是围绕一个 okhttp 请求并进行一些额外的处理/解析。通常我只会获取结果(资源)并在完成 lambda 内部处理它,但由于我需要这两个值,我不能在这里这样做。我试图做类似的东西这个,但我的asyncRequest函数没有返回类型,所以我没有做异步方式/等待链接的方式做。
你可以用Coroutines和Flow来做这样的事情:
使用块将回调转换为可挂起的函数suspendCancellableCoroutine {...}:
suspend fun <T> request(requestBuilder: RequestBuilder): T = suspendCancellableCoroutine { cont ->
Server.asyncRequest(requestBuilder) { resource: T, error: ServiceError? ->
if(error != null)
cont.resumeWithException(error) // Makes the Flow throw an exception
else
cont.resume(resource) // Makes the Flow emit a correct result
}
}
Run Code Online (Sandbox Code Playgroud)
创建一个Flow来发出第一个请求:
val resourceOneFlow = flow {
emit(request<String>(RequestBuilder(endpoints.second, ids, params)))
}
Run Code Online (Sandbox Code Playgroud)
创建一个Flow来发出第二个请求:
val resourceTwoFlow = flow {
emit(request<String>(RequestBuilder(endpoints.third, ids, params)))
}
Run Code Online (Sandbox Code Playgroud)
将两个Flows与zip运算符结合起来:
val requestsResultFlow = resourceOneFlow.zip(resourceTwoFlow) { resourceOne, resourceTwo ->
// Build whatever you need with resourceOne and resourceTwo here and let it flow
"$resourceOne $resourceTwo".length // Here I concatenate both strings and return its length
}
Run Code Online (Sandbox Code Playgroud)
使用操作符激活/启动Flowcollect并使用其结果:
requestsResultFlow.collect { length ->
// Consume the result here
println("$length") // Here I print the number received
}
Run Code Online (Sandbox Code Playgroud)
您可以在此处查看Flow文档。