https://kotlinlang.org logo
#android-architecture
Title
# android-architecture
m

mgrazianodecastro

02/22/2023, 3:45 PM
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

ephemient

02/23/2023, 12:10 AM
c

curioustechizen

02/23/2023, 7:10 AM
Agree with ^. Hitting an endpoint periodically is not a ViewModel concern, it is a data layer concern. WorkManager is best suited for this.
m

mgrazianodecastro

02/23/2023, 8:54 AM
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
8 Views