让您等待另一个方法,直到协同程序完成[Android Kotlin]
我想让wait方法让您等待另一个方法,直到协同程序完成[Android Kotlin],android,kotlin-coroutines,coroutine,Android,Kotlin Coroutines,Coroutine,我想让wait方法getCurrentUser或通知该方法,直到协同路由完成。因为方法中使用了在协同程序中初始化的变量conversationKit。协同程序在方法integrateConversationKit中启动。这两种方法都在MainActivity中的onCreate中调用。但我得到的错误是lateinit属性conversationKit尚未初始化 class ConversationKitService(private val context: Context) {
getCurrentUser
或通知该方法,直到协同路由完成。因为方法中使用了在协同程序中初始化的变量conversationKit
。协同程序在方法integrateConversationKit
中启动。这两种方法都在MainActivity
中的onCreate
中调用。但我得到的错误是lateinit属性conversationKit尚未初始化
class ConversationKitService(private val context: Context) {
private val scope = CoroutineScope(Dispatchers.Main)
lateinit var conversationKit: ConversationKit
fun integrateConversationKit() {
val job = scope.launch {
val result = conversationKitFactory.create(
settings = settings
)
when (result) {
is ConversationKitResult.Success -> conversationKit = result.value
is ConversationKitResult.Failure -> println(result.message)
}
}
}
fun getCurrentUser(): User? {
return conversationKit.getCurrentUser()
}
}
您可以使用
流
s以反应方式更新conversationKit
,然后返回它的第一个非空值
class ConversationKitService(private val context: Context) {
private val scope = CoroutineScope(Dispatchers.Main)
private val conversationKit = MutableStateFlow<ConversationKit?>(null)
fun integrateConversationKit() {
val job = scope.launch {
val result = conversationKitFactory.create(
settings = settings
)
when (result) {
is ConversationKitResult.Success -> conversationKit.value = result.value
is ConversationKitResult.Failure -> println(result.message)
}
}
}
suspend fun getCurrentUser() = conversationKit.first{ it != null }.getCurrentUser()
}
class ConversationKitService(私有val上下文:上下文){
private val scope=CoroutineScope(Dispatchers.Main)
private val conversationKit=MutableStateFlow(null)
fun IntegratedConversationKit(){
val job=scope.launch{
val结果=conversationKitFactory.create(
设置=设置
)
何时(结果){
是ConversationKitResult.Success->conversationKit.value=result.value
是ConversationKitResult.Failure->println(result.message)
}
}
}
suspend fun getCurrentUser()=conversationKit.first{it!=null}.getCurrentUser()
}
我只想让getCurrentUser
成为一个挂起调用,从缓存结果的内部挂起调用中获取ConversationKit
class ConversationKitService(private val context: Context) {
@Volatile
private var conversationKit: ConversationKit? = null
...
suspend fun getCurrentUser() = getConversationKit()?.getCurrentUser()
private suspend getConversationKit(): ConversationKit? {
if (conversationKit != null) return conversationKit
val result = conversationKitFactory.create(settings = settings)
conversationKit = when (result) {
is ConversationKitResult.Success -> result.value
is ConversationKitResult.Failure -> null
}
return conversationKit
}
}
然后,null
值意味着检索该值时出错。您还可以将其包装为某种类型的结果
,以便返回成功/失败,而不是可为空的值
但是,如果您真正想做的是使getCurrentUser
成为一个无需协同程序即可使用的非挂起阻塞调用,那么您可以使用runBlocking
:
class ConversationKitService(private val context: Context) {
@Volatile
private var conversationKit: ConversationKit? = null
...
fun getCurrentUser() = runBlocking {
getConversationKit()?.getCurrentUser()
}
private suspend getConversationKit(): ConversationKit? {
if (conversationKit != null) return conversationKit
val result = conversationKitFactory.create(settings = settings)
conversationKit = when (result) {
is ConversationKitResult.Success -> result.value
is ConversationKitResult.Failure -> null
}
return conversationKit
}
}
不过,我并不推荐这种方法。为什么不把
getCurrentUser
设为一个挂起函数,然后从那里调用integrateConversionKit()
?它看起来不像是从onCreate
调用的。您在哪里调用getCurrentUser
函数?为什么我不调用integrateConversionKit(),因为变量conversationKit是全局必需的,需要使用此变量调用其他挂起函数。因此,这两种方法都是在onCreate中调用的。我无法理解您的用例。我同意@OhhhThatVarun:如果您希望其他方法等待conversationKit
变量初始化,您只需将getCurrentUser
本身设置为一个挂起函数,然后重新构造其余的代码即可。