Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
Se borra el valor del observador al usar la empuñadura de daga

Soy nuevo en empuñadura de daga (DI) e implemento empuñadura de daga (DI) en mi proyecto. Ahora estoy tratando de inyectar ViewModel. Funciona bien. Pero en el resultado de la API, estoy usando mutableLivedata para actualizar el valor del modelo de vista para verlo con el observador. El observador escucha y obtiene el valor que funciona bien. Pero leí el valor de los datos observados; borró el valor. No sé por qué sucedió. ¿Alguien puede ayudarme a encontrar esto?

Fragmento de inicio de sesión

 @AndroidEntryPoint class LoginFragment : BaseFragment<FragmentLoginBinding>(FragmentLoginBinding::inflate) { private val viewModel by viewModels<LoginViewModel>() override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) viewActionListeners() setUpViewModelObserver() } private fun setUpViewModelObserver() { viewModel.users.observe(viewLifecycleOwner) { response -> AppLog.e("validateResponse", response.toString()) when (response.status) { Status.SUCCESS -> { binding.frmLayoutProgress.visible(false) response.data?.let { users -> AppLog.e("AuthenticationMethod",users.clientResponse.authenticationMethod) } } Status.LOADING -> { binding.frmLayoutProgress.visible(true) } Status.ERROR -> { //Handle Error binding.frmLayoutProgress.visible(false) AppLog.e("Error:", response.message.toString()) } } } } }

Si comenta la línea AppLog.e("AuthenticationMethod",users.clientResponse.authenticationMethod) } , devolverá el valor si la respuesta sin comentarios se establece en nulo.

LoginViewModel

 @HiltViewModel class LoginViewModel @Inject constructor( private val repository: LoginRepository, private val networkHelper: NetworkHelper ) : ViewModel() { val users: MutableLiveData<Resource<ResponseValidateUser>> = MutableLiveData() fun loadValidateUser(email: String) { viewModelScope.launch { users.postValue(Resource.loading(null)) if (networkHelper.isNetworkConnected()) { repository.getValidateUser(email).let { if (it.isSuccessful) { users.postValue(Resource.success(it.body())) } else users.postValue(Resource.error(it.errorBody().toString(), null)) } } else users.postValue(Resource.error("No internet connection", null)) } } }

Iniciar sesiónRepositorio

 class LoginRepository @Inject constructor(private val apiHelper: AuthApiHelper) { suspend fun getValidateUser(email: String) = apiHelper.getValidateUser(email) }

AuthApiHelper

 interface AuthApiHelper { suspend fun getValidateUser(email: String): Response<ResponseValidateUser> }

RespuestaValidarUsuario

 data class ResponseValidateUser( @SerializedName("clientResponse") val clientResponse: ClientResponse, @SerializedName("status") val status: String, @SerializedName("errorMessage") val errorMessage: String )
over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!