liveData with Coroutines
See original GitHub issueHi! who already use for this one androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha01
? I’ve tried it into my repository layer but doesn’t work (i’m using retrofit 2.6.0
). this is my code:
interface Service {
@GET("/data")
suspend fun getData(): Data
}
class Repository @Inject constructor(val service: Service) {
fun getData(): LiveData<Data> = liveData {
val data = service.getData()
emit(data)
}
}
and this is my viewmodel:
...
fun fetch() {
viewModelScope.launch {
val data = repository.getData()
println(data.value)
}
}
...
the result is null
.
any idea about this? thanks!
Issue Analytics
- State:
- Created 4 years ago
- Comments:6
Top Results From Across the Web
Use Kotlin coroutines with lifecycle-aware components
The liveData building block serves as a structured concurrency primitive between coroutines and LiveData . The code block starts executing when ...
Read more >LiveData with Coroutines and Flow — Part II - Medium
This is one of the most common ways to launch a coroutine because most data operations begin in the ViewModel. With the viewModelScope...
Read more >Coroutines with Lifecycle and LiveData - RayWenderlich.com
In this tutorial, you'll build an Android app that uses coroutines with LiveData objects and lifecycle-aware CoroutineScopes.
Read more >LiveData with Coroutines (part I - one-shot operations)
LiveData with Coroutines (part II - parameters with switchMap) · LiveData with Coroutines and Flow (Android Dev Summit '19) · Kotlin Andriod MVVM ......
Read more >LiveData with Coroutines and Flow (Android Dev Summit '19)
LiveData is a simple lifecycle-aware observable, designed for making UIs that react to changes safely and efficiently.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@GuilhE I think you can do something like this
Let me know if it helped you.
Hi @isfaaghyth . Your liveData from repository.getData() don`t have active observers. So liveData not emiting data. Try something this.
P.S. Correct me if something is wrong