android - Cooutin 执行任务
问题描述
嗨,这是我的用户存储库
class UserRepository(private val appAuth: FirebaseAuth) : SafeAuthRequest(){
suspend fun userLogin(email: String,password: String) : AuthResult{
return authRequest { appAuth.signInWithEmailAndPassword(email,password)}
}
}
这是 SafeAuthRequest 类
open class SafeAuthRequest {
suspend fun<T: Any> authRequest(call : suspend () -> Task<T>) : T{
val task = call.invoke()
if(task.isSuccessful){
return task.result!!
}
else{
val error = task.exception?.message
throw AuthExceptions("$error\nInvalid email or password")
}
}
}
调用类似的东西
/** Method to perform login operation with custom */
fun onClickCustomLogin(view: View){
authListener?.onStarted()
Coroutines.main {
try {
val authResult = repository.userLogin(email!!,password!!)
authListener?.onSuccess()
}catch (e : AuthExceptions){
authListener?.onFailure(e.message!!)
}
}
}
我的 authListener 像这样
interface AuthListener {
fun onStarted()
fun onSuccess()
fun onFailure(message: String)
}
由于任务未完成,我收到错误消息
是执行任务的正确方法
解决方案
我正在使用 MVVM 架构模式,所以我要提供的示例是从我的ViewModel
类中调用的,这意味着我可以访问viewModelScope
. 如果要在Activity
类上运行类似的代码,则必须使用可用于 Activity 的 Coroutines 范围,例如:
val uiScope = CoroutineScope(Dispatchers.Main)
uiScope.launch {...}
回答您的问题,我为从用户存储库检索登录所做的工作是:
//UserRepository.kt
class UserRepository(private val appAuth: FirebaseAuth) {
suspend fun userLogin(email: String, password: String) : LoginResult{
val firebaseUser = appAuth.signInWithEmailAndPassword(email, password).await() // Do not forget .await()
return LoginResult(firebaseUser)
}
}
LoginResult 是 firebase 身份验证响应的包装类。
//ClassViewModel.kt
class LoginFirebaseViewModel(): ViewModel(){
private val _loginResult = MutableLiveData<LoginResult>()
val loginResult: LiveData<LoginResult> = _loginResult
fun login() {
viewModelScope.launch {
try {
repository.userLogin(email!!,password!!).let {
_loginResult.value = it
}
} catch (e: FirebaseAuthException) {
// Do something on firebase exception
}
}
}
}
Activity 类的代码是这样的:
// Function inside Activity
fun onClickCustomLogin(view: View){
val uiScope = CoroutineScope(Dispatchers.Main)
uiScope.launch {
try {
repository.userLogin(email!!,password!!).let {
authResult = it
}
}catch (e : FirebaseAuthException){
// Do something on firebase exception
}
}
}
使用协程的主要好处之一是您可以按顺序转换异步代码。这意味着您不需要侦听器或回调。
我希望这对你有帮助
推荐阅读
- java - 使用范围 JAVA 添加所有奇数的总和
- ajax - 无法从 Ajax Jquery 调用控制器操作方法
- asp.net-mvc - WebDeploy 包包含个人文件夹路径
- javascript - Chrome 在 Angular 中使用 resumablejs 上传大于 1.5 GB 的文件时崩溃
- php - PHP 中的 Rest API:{'error': "Missing 'inputs' or 'instances' key"}
- firebase - 无法在 Firebase 中创建新用户属性
- amazon-web-services - AWS EC2 Autoscaling Target Tracking 策略如何扩展?
- javascript - [JS/HTML/PHP]刷新页面后的特定div
- c# - DryIoc:将请求范围服务的代理注入单例
- javascript - 为什么 window.getSelection() 不工作?它不会占用文本