I have a Service that loads data from via http request every second.
I am wondering how to implement the service in my current MVVM structure.
A solution I could think of is to:
View calls method from ViewModel
ViewModel calls method from Repository
Start the Service in the Repository and pass in some LiveData, as Repositories should be responsible for data loading.
Update this LiveData in the Service via postValue().
Observe this LiveData in UI and notify the ViewModel of the changes
In ViewModel get the changed LiveData and update the other LiveData for the View accordingly
Stop the Service based on the outcome of step 6
However, I am wondering if there is no better solution with a more direct communication from Service to ViewModel or even from Service to Repository to ViewModel
Related
I have a FirebaseMessagingService which when receiving a new Message should make a network call to retrieve some data to show in a Fragment.
To do so I implemented a MVVM pattern, resulting in a Fragment, a ViewModel and a Repository.
Right now I am directly calling the Repository from within the Service to retrieve the needed data. Unfortunately because it's not the ViewModel doing the call the ViewModel's data (observable) is not being updated, thus the Fragment too.
I know I should not pass a reference to a ViewModel, especially not from a service.
So my question is : how do I update the data in the ViewModel if the Repository is not called from that ViewModel ?
So my entrypoint is the service, but the end result should be that my fragment is being updated with fresh data !
As you said, it is not recommended to call a ViewModel from a Service directly! But here is another BAD solution you can use; you can use Message Bus to communicate between that service and your ViewModel or Fragment; when your Service receives a new message, it can publish an event on the message bus indicating that new data is available. The ViewModel can then subscribe to this event and fetch the data from the Repository when the event is received. Once the data is fetched, the ViewModel can update its observable data, triggering the Fragment to update.
Other than that, you can broadcast a message from the service and register a broadcast receiver in your fragment. When you get the broadcast, you can then ask the viewModel to update and fetch the data!
So, I am using workmanager to retrieve data from an FTP server. I have to display one type or information or another type deppending on the state of the worker.
I was using getWorkInfoByIdLiveData on the viewmodel and I was observing the state of the worker on the activity. But, as the Observer is almost deprecated I am trying to use Flow instead.
The problem that I have is that I don't know why, I can't use the asStateFlow() on the livedata. enter image description here
Does anyone know a way to observe the state of a worker without using LiveData and Observer?
Thank youuu
I have an activity (MainActivity) and I am using ViewModel Live data for UI update. I want to use Retrofit for network calls.
If I use service how can i use LiveData in service ,is there a way for this.
You should not use a Retrofit in a Service, as it (Service) is not intended for this purpose (in most situations).
If you have a small application, you can, bypassing the clean architecture, make calls directly to the ViewModel and use the LiveData there.
Right now I am trying to create a process, where I want to insert data in table and some observers will get notified and able to edit the data upon their interest. Below is a rough idea on how to do it. Can anyone please suggest the model/arch how I can achieve this?
Follow this tutorial on Room, ViewModel, and LiveData.
Room is a SQL database abstraction which can expose its data through LiveData. ViewModel is a class that holds data for the UI and survives configuration changes. LiveData is an observable container for data that is aware of the android lifecycle so you don't have to manage it in the lifecycle callbacks.
Basically, you create a Room database then expose LiveData objects to the ViewModel. The ViewModel in turn exposes LiveData objects to the fragment/activity. The fragment or activity observes the ViewModel's LiveData by attaching an Observer. The Observer defines how the fragment/activity reacts to changes in the data.
If you prefer RXJava you can follow this tutorial instead. It's essentially the same, but instead of exposing data with LiveData you use reactive streams.
Edit: here's a really good article on architecture: https://proandroiddev.com/android-architecture-starring-kotlin-coroutines-jetpack-mvvm-room-paging-retrofit-and-dagger-7749b2bae5f7
I want to request a sync of my sync adapter every time a change happens to some of my data. Having LiveData I wonder if the correct way would be to bind a observer to the data within the application lifecycle.
Does anyone know what the "official" way of doing this is?