Observe LiveData from JobService

4.6k views Asked by At

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.

1

There are 1 answers

1
alekop On

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)
    }
})