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

118
Views
Especifique un objeto LiveData existente para la función de creación de kotlin de liveData

Por ejemplo, podemos crear un objeto LiveData usando una función de construcción de liveData :

 var liveData: LiveData<String> = liveData(Dispatchers.IO) { delay(1000) emit ("hello") delay(1000) emit ("world") }

Esto es útil en ViewModel , creo un objeto LiveData que recibe múltiples valores de datos de una función asincrónica pesada en una línea.

Pero, ¿qué pasa si ya tengo un objeto MutableLiveData y solo quiero publicar valores allí, sin crear un nuevo objeto LiveData ? ¿Cómo puedo hacer esto?

Necesito una forma de emitir de forma asíncrona múltiples valores en el objeto MutableLiveData existente dentro de un alcance de ViewModel para finalizar automáticamente todas las tareas en ejecución cuando se borre ViewModel.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede publicar en MutableLiveData en una rutina iniciada desde viewModelScope .

 private val myMutableLiveData = MutableLiveData<String>() fun someFun() { viewModelScope.launch { myMutableLiveData.value = “Hello” delay(500L) myMutableLiveData.value = “World” myMutableLiveData.value = someSuspendFunctionRetriever() val result = withContext(Dispatchers.IO) { someBlockingCallRetriever() } myMutableLiveData.value = result // or use post() when not on Main dispatcher: withContext(Dispatchers.IO) { val result = someBlockingCallRetriever() myMutableLiveData.post(result) } } }
over 4 years ago · Santiago Trujillo Report
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!