Skip to content Skip to sidebar Skip to footer

Mvvm Architecture In Android Using Volley

I'm studying the MVVM to see if it can help me for my upcoming projects. What I understand so far, is that I need to use a ViewModel for holding my UI datas. I also need to use a R

Solution 1:

Instead of passing the Application to your MyRepository's constructor and creating ApiRequest, you can pass the ApiRequest to MyRepository's constructor.

publicMyRepository(ApiRequest apiRequest){
    this.apiRequest = apiRequest;
}

Now the MyRepository has no reference to Context.

And, regarding ViewModel having direct reference to MyRepository, you can do dependency inversion:

Create an interface, for instance, MyDataStore with the method getPojo(). MyRepository will implement this interface. While creating MyViewModel, you will pass the MyRepository to it, but MyViewModel will only have reference to MyDataStore.

interfaceMyDataStore {
   ... getPojo()
}

publicclassMyRepositoryimplementsMyDataStore {
   ...
}

publicMyViewModel(MyDataStore dataStore) {
        this.dataStore = dataStore;
        this.pojo = newMutableLiveData<>();
}

Solution 2:

The LifeCycle library provides the AndroidViewModel component, which is just an Application's context aware ViewModel, pretty close to Bob's answer, and did the job here without memory leaks hazards.

Post a Comment for "Mvvm Architecture In Android Using Volley"