使用Volley的android中的MVVM架构 [英] MVVM architecture in android Using Volley
问题描述
我正在研究MVVM,看它是否可以为我的后续项目提供帮助.到目前为止,我所了解的是,我需要使用 ViewModel 用于保存我的UI数据.我还需要使用一个Repository类来执行所有对WebServices的请求,并且我正在使用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 Repository class to perform all my Requests to WebServices, and I'm using the Volley Library.
这就是我所做的:
public class MyViewModel extends ViewModel {
private MyRepository repository;
private MutableLiveData<MyPojo> pojo;
public MyViewModel(MyRepository repository) {
this.repository = repository;
this.pojo = new MutableLiveData<>();
}
public LiveData<MyPojo> updatePojo(){
pojo.postValue(repository.getPojo());
return pojo;
}
}
存储库类
public class MyRepository {
private Application application;
private LiveData<MyPojo> pojo;
public MyRepository(Application application) {
this.application = application;
}
public MyPojo getPojo(){
if(pojo == null){
ApiRequest apiRequest = new ApiRequest(ApiSingleton.getInstance(application).getRequestQueue(), application);
apiRequest.apiGetRequest(ApiRequest.MY_ENDPOINT, null, new ApiRequest.apiCallback() {
@Override
public void onSuccess(Context context, JSONObject jsonObject) {
pojo = ApiResponseParser.parse(jsonObject, MyPojo.class);
}
@Override
public void onError(Context context, String message) {
}
});
}
return pojo;
}
}
在此处指定了
It's specified here that a ViewModel must never reference a view, Lifecycle, or any class that may hold a reference to the activity context. As you can see, I must use a context in order to perform a Volley request in my Repository class, and my ViewModel has a reference on this class.
我在设计中缺少什么吗?排球在这里不兼容吗?
Am I missing something in my design? Is Volley not compatible here?
推荐答案
而不是将 Application
传递到MyRepository的 constructor
并创建 ApiRequest
,您可以将 ApiRequest
传递给MyRepository的 constructor
.
Instead of passing the Application
to your MyRepository's constructor
and creating ApiRequest
, you can pass the ApiRequest
to MyRepository's constructor
.
public MyRepository(ApiRequest apiRequest) {
this.apiRequest = apiRequest;
}
现在 MyRepository
没有对 Context
的引用.
而且,关于直接引用 MyRepository
的 ViewModel
,您可以依赖反转:
And, regarding ViewModel
having direct reference to MyRepository
, you can do dependency inversion:
使用方法 getPojo()
创建一个接口
,例如 MyDataStore
. MyRepository
将实现此接口
.创建 MyViewModel
时,您会将 MyRepository
传递给它,但是 MyViewModel
将仅引用 MyDataStore
./p>
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
.
interface MyDataStore {
... getPojo()
}
public class MyRepository implements MyDataStore {
...
}
public MyViewModel(MyDataStore dataStore) {
this.dataStore = dataStore;
this.pojo = new MutableLiveData<>();
}
这篇关于使用Volley的android中的MVVM架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!