question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ViewModels request new data each time view is recreated

See original GitHub issue

If PlantListFragment or GardenPlantingListFragment are recreated, their ViewModels make a new database request instead of returning the last value held by the livedata.

For example:

fun getGardenPlantings() = gardenPlantingRepository.getGardenPlantings()

fun getPlantAndGardenPlantings() = gardenPlantingRepository.getPlantAndGardenPlantings()

The Arch docs say this is a not to do this (link):

    private LiveData<String> getPostalCode(String address) {
       // DON'T DO THIS
       return repository.getPostCode(address);
    }

The UI component then needs to unregister from the previous LiveData object and register to the new instance each time it calls getPostalCode(). In addition, if the UI component is recreated, it triggers another call to the repository.getPostCode() method instead of using the previous call’s result.

The Arch components BasicSample shows a better way to implement this using MediatorLiveData (link)

When I implemented this myself, using the MediatorLiveData also fixed #10 https://github.com/chris-feldman/android-sunflower/blob/master/app/src/main/java/com/google/samples/apps/sunflower/viewmodels/PlantListViewModel.kt

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
tiembocommented, Jun 7, 2018

Hi @sagar-viradiya - yes, please open a new issue and we can discuss the livedata changes there.

1reaction
chris-feldmancommented, Jun 6, 2018

Sure, I’ll see if I can get one together today 👍

On Tue, Jun 5, 2018, 11:21 PM Tiem Song notifications@github.com wrote:

Thanks for the detailed write-up! Would you be interested in opening a pull request to update the two list ViewModels?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/googlesamples/android-sunflower/issues/24#issuecomment-394927733, or mute the thread https://github.com/notifications/unsubscribe-auth/AWQj3YRw8qHSnL3g4uRi4eWNSlj-EeK1ks5t50q4gaJpZM4UbGgs .

Read more comments on GitHub >

github_iconTop Results From Across the Web

ViewModels request new data each time view is recreated #24
The UI component then needs to unregister from the previous LiveData object and register to the new instance each time it calls ...
Read more >
android - ViewModel refetches data when fragment is recreated
When the user navigates from one item to another(via Bottom navigation) and back again view model call repository function to fetch data again....
Read more >
ViewModel overview - Android Developers
ViewModel lets you manage your UI's data in a lifecycle-aware fashion.
Read more >
ViewModels: Persistence, onSaveInstanceState(), Restoring ...
ViewModels hold transient data used in the UI but they don't persist data. Once the associated UI Controller (fragment/activity) is destroyed or ...
Read more >
Using ViewModels in Android to preserve your running app's ...
As you can see, the ViewModel scope stays alive throughout your app Activity/Fragment's lifecycle and is only destroyed when the Activity/Fragment is destroyed....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found