Reputation: 4841
I have a repository which holds the LiveData
object and is used by both Activity
and now it's needed in JobService
(From Firebase dispatcher) through a ViewModel
.
There is answer for plain Service
over here: Observe LiveData from foreground service
But it doesn't mention how to do the same for JobService
.
Upvotes: 12
Views: 4657
Reputation: 3026
If you want to observe a LiveData object from something that isn't a LifecycleOwner, you can use the observeForever
method.
val data = getLiveDataFromSomewhere()
data.observeForever(object: Observer<Whatever> {
override fun onChanged(stuff: Whatever?) {
// do something with stuff
data.removeObserver(this)
}
})
Upvotes: 12