Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communication of Android Services with Activity in MVVM (Model View View Model)

I have just started using MVVM in android using kotlin. I have created an Android Service which is just fetching user location. This service runs constantly on background and uses FusedLocationApi to fetch the location. I have been reading articles of using background services with MVVM. The most helpful article I found is this Android Service MVVM communicaton. It says that you have to use repository when android service and view model communicates. But I have also found articles that says that do no use Android Framework classes in View Model (Do not use Android Classes in View Model). I am totally confused that how you communicate android services with activity in MVVM. Either we have to use repository for it or how we can implement it.

Any help will be appreciated,

like image 532
Developer Avatar asked Nov 16 '22 19:11

Developer


1 Answers

I think the post that you linked above is trying to say: - Service has instance of the repository - ViewModel has instance of the repository - When data changes in service it needs to trigger repository (set new value there) - This will trigger listeners in view model automatically because it is live data or it is some other reactive (observable) data source

like image 104
uberchilly Avatar answered Nov 19 '22 10:11

uberchilly