我正在尝试通过我的代码通过Google登出:
suspend fun signOut(context: Context): Boolean = with(Dispatchers.IO) {
try {
val signOutTask = GoogleSignIn.getClient(context,
getGoogleSignInOptionsBuilder()).signOut()
Tasks.await(signOutTask)
true
} catch (e: ExecutionException) {
false
} catch (e: InterruptedException) {
false
}
}
signOutTask是Task(Void),我希望它同步返回。但在以下行中:
Tasks.await(signOutTask)
它显示了不适当的阻塞方法调用感谢您的帮助!
最佳答案
当将Kotlin Coroutines与Google Play服务的API结合使用时,您应该使用 kotlinx-coroutines-play-services
,它可以正确完成工作,将Task
API转换为suspend
API,而不会阻塞线程:
// In your dependencies block of your build.gradle
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.3.9"
这使您可以将代码编写为:suspend fun signOut(context: Context): Boolean = try {
val signOutTask = GoogleSignIn.getClient(context,
getGoogleSignInOptionsBuilder()).signOut()
signOutTask.await()
true
} catch (e: Exception) {
false
}
关于android - Tasks.await(task)显示不适当的阻塞方法调用警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63678229/