【发布时间】:2019-01-26 02:31:54
【问题描述】:
我正在尝试使用协程在改造中实现 JWT 令牌身份验证和刷新令牌。令牌存储在 Room 数据库中。我应该如何实现 await 调用?
目前我正在使用 runBlocking {...} 调用来等待异步本地/远程响应
例子:
client.addInterceptor {
val accessToken = runBlocking { tokenRepository.getActiveToken() }?.access_token ?: "-"
val request = it.request()
.newBuilder()
.addHeader("Authorization", "Bearer $accessToken")
.build()
return@addInterceptor it.proceed(request)
}
我想遵循的是传统模式:
launch {
withContext(IO){...}
}
我应该怎么做?
【问题讨论】:
-
我在 Authenticator 中遇到了与 runBlocking{} 类似的问题,想知道我在这个线程中的回答是否相关:stackoverflow.com/questions/62950438/… 我更幸运的是用 Retrofit 的 Call
替换了协程并执行同步请求。
标签: android retrofit2 kotlinx.coroutines