Android MVVM repository update onResume

  android, kotlin, lifecycle, mvvm, repository

In the small app I’m developing I want the users to have the option to turn automatic updates on and off. So that when activated, the data in the repository is updated every, lets say, 1 hour. And if turned off, the data will refresh only when the users open the app, and at least 30 minutes have passed since last time.

The first scenario, when automatic updates is on, is already covered with a CoroutineWorker. But the second I don’t have quite clear how to do it. I mean, I could simply call a function from Fragment to ViewModel, and then the Viewmodel do the call if necessary from repository. But I don’t know if that would be a proper way. First I considered implementing LifecycleObserver in ViewModel or Repository. But I’m not sure if that’s a good design.

So that’s my question. How to update database (Room) in repository conditionally on Fragments lifecycle.

Thanks in advance.

Source: Android Questions

LEAVE A COMMENT