【发布时间】:2020-03-10 16:49:31
【问题描述】:
将应用置于后台后,Kotlin 通道停止发送事件(不要保持活动启用)
class UserRepositoryImpl(
private val userRequestDataSource: UserRequestDataSourceContract,
) : UserRepositoryContract {
private var loginToken: LoginTokenDecode? = null
private val authChannel by lazy { Channel<Boolean?>() }
override suspend fun requestLogin(userLoginBo: UserLoginRequestBo){
// isClosedForSend is true after putting app in background
if(!authChannel.isClosedForSend) {
authChannel.send(true)
}
}
视图模型
class UserViewModel : ViewModel {
init {
authChannelUc.invoke(scope = viewModelScope, onResult = ::authenticated)
}
...
}
【问题讨论】:
-
用户存储库合同的范围或生命周期是多少?
-
@GiorgosNeokleous 视图模型范围
标签: android kotlin channel kotlin-coroutines