I have a separate class in which I handle data fetching (specifically Firebase) and I usually return LiveData objects from it and update them asynchronously. Now I want to have the returned data stored in a ViewModel, but the problem is that in order to get said value, I need to observe the LiveData object returned from my data fetching class. The observe method required a LifecycleOwner object as the first parameter, but I obviously don't have that inside of my ViewModel and I know I am not supposed to keep a reference to the Activity/Fragment inside of the ViewModel. What should I do?


Solution 1:

In this blog post by Google developer Jose Alcérreca it is recommended to use a transformation in this case (see the "LiveData in repositories" paragraph) because ViewModel shouldn't hold any reference related to View (Activity, Context etc) because it made it hard to test.

Solution 2:

In ViewModel documentation

However ViewModel objects must never observe changes to lifecycle-aware observables, such as LiveData objects.

Another way is for the data to implement RxJava rather than LiveData, then it won't have the benefit of being lifecycle-aware.

In google sample of todo-mvvm-live-kotlin, it uses a callback without LiveData in ViewModel.

I am guessing if you want to comply with the whole idea of being lifecycle-ware, we need to move observation code in Activity/Fragment. Else, we can use callback or RxJava in ViewModel.

Another compromise is implement MediatorLiveData (or Transformations) and observe (put your logic here) in ViewModel. Notice MediatorLiveData observer won't trigger (same as Transformations) unless it's observed in Activity/Fragment. What we do is we put a blank observe in Activity/Fragment, where the real work is actually done in ViewModel.

// ViewModel
fun start(id : Long) : LiveData<User>? {
    val liveData = MediatorLiveData<User>()
    liveData.addSource(dataSource.getById(id), Observer {
        if (it != null) {
            // put your logic here
        }
    })
}

// Activity/Fragment
viewModel.start(id)?.observe(this, Observer {
    // blank observe here
})

PS: I read ViewModels and LiveData: Patterns + AntiPatterns which suggested that Transformations. I don't think it work unless the LiveData is observed (which probably require it to be done at Activity/Fragment).

Solution 3:

I think you can use observeForever which does not require the lifecycle owner interface and you can observe results from the viewmodel