可变实时数据给出多重响应给出来自 api 的旧响应,然后给出新响应

Mutable live-data give multiple response give old response from api and after give new response

** 可变实时数据给出多重响应来自 api 的旧响应,然后给出新响应 **

可变实时数据给出多重响应

这是我的代码

存储库

suspend fun forgotPassword(email:String,sig: String,salt: String):Response<ForgotDataClass>{
    return RetrofitInstance.api.forgotPassword(email,sig,salt)
}

查看模型

class ForgotPasswordViewModel(private val repository: Repository) : ViewModel() {

var myPassword: MutableLiveData<Response<ForgotDataClass>> = MutableLiveData()
fun forgotPassword(email: String, sig: String, salt: String) {
    viewModelScope.launch {
        val response = repository.forgotPassword(email, sig, salt)
        myPassword.value = response
    }
}

}

api界面

@POST("forgot_password.php")
 suspend fun forgotPassword(
    @Query("email") email:String,
    @Query("sig") sig:String,
    @Query("salt") salt:String
):Response<ForgotDataClass>

主要activity

viewModel.forgotPassword(email,sig1,salt1)
                    viewModel.myPassword.observe(this, { response ->
                        if (response.isSuccessful){
                            when(response.body()?.status){
                                "1" -> {
                                    finish()
                                    Toast.makeText(this, "your password reset successfully", Toast.LENGTH_SHORT).show()
                                }
                                "-2" -> {
                                    Toast.makeText(this, "Email is not exit ", Toast.LENGTH_SHORT).show()
                                }
                                else -> {
                                    Toast.makeText(this, "Try again something went wrong", Toast.LENGTH_SHORT).show()
                                }
                            }
                        } else {
                            Toast.makeText(this, "Try again something went wrong", Toast.LENGTH_SHORT).show()
                        }

你可以试试这个。您可以像这样 return 您的 LiveData 我认为您不需要全局变量。并在使用不同的线程更新时使用 postValue MutableLiveData

class ForgotPasswordViewModel(private val repository: Repository) : ViewModel() {

    fun forgotPassword(email: String, sig: String, salt: String): LiveData {
        var myPassword = MutableLiveData<Response<ForgotDataClass>>()
        viewModelScope.launch {
            myPassword.postValue(repository.forgotPassword(email, sig, salt))
        }
        return myPassword
    }
}

在你的fragment/activity中你可以这样调用

viewModel.forgotPassword(email,sig1,salt1).observe(this) { response ->
    // do stuff with your result
}

编辑

要处理错误,您可以像这样将调用包装在 try-catch 块中,然后捕获并处理这些错误。

    fun forgotPassword(email: String, sig: String, salt: String): LiveData {
        var myPassword = MutableLiveData<Response<ForgotDataClass>>()
        viewModelScope.launch {
            try {
                myPassword.postValue(repository.forgotPassword(email, sig, salt))
            } catch(e: HttpException) {
               // handle
            }
        }
        return myPassword
    }
}