I need to hit an endpoint to update the room datab...
# android-architecture
m
I need to hit an endpoint to update the room database every some amount of time. The way I designed this is to turn the call stack containing the loop into a Job, created by a coroutine scope. The question I have in mind is: where is the best place to start this job, at the view model or at the activity? If feels more natural to me to think that view model is the way to go, specially in single activity applications with dozen of navigation. But, since I'm using Hilt, during navigation of screens that share the same view model the instance is being destroyed every single time, so the Job finishes and starts again at each route. Any ideas on how I can improve my solution?
e
c
Agree with ^. Hitting an endpoint periodically is not a ViewModel concern, it is a data layer concern. WorkManager is best suited for this.
m
Yeah, indeed view model feels like a anti pattern. The only thing I'm worried with work manager is to control that it actually gets executed properly. I already have I notification being displayed by a foreground service, and I hope I don't need another one. I'm gonna read the section about "what is considered a long running task" But I will give it a try today
Copy code
Long Running: Tasks which might run for longer, potentially longer than 10 minutes.
that's great news for me
thanks you all