繁体   English   中英

LiveData,MVVM和存储库模式

[英]LiveData, MVVM and Repository Pattern

这是一个好方法还是我刚刚找到了一个讨厌的解决方法?

我正在使用MediatorLiveData类,因为似乎对更新LiveData对象的源很有用。

我的意思是,我在互联网上找到的大多数教程Livedata使用LivedataMutableLivedata而没有动态源,例如:

fun search(/*no input params*/): Call<List<Person>>

但就我而言,我拥有以下按名称执行搜索的Web服务:

interface APIServidor {
    @GET("search")
    fun search(@Query("name") name: String): Call<List<Person>>

}

public class PeopleRepository {


    public LiveData<List<Person>> search(String name){

        final MutableLiveData<List<Person>> apiResponse = new MutableLiveData<>();
        Call<List<Person>> call = RetrofitService.Companion.getInstance().getApiServer().search(name);
        call.enqueue(new Callback<List<Person>>() {
            @Override
            public void onResponse(@NonNull Call<List<Person>> call, @NonNull Response<List<Person>> response) {
                if (response.isSuccessful()) {
                    apiResponse.postValue(response.body());
                }
            }

            @Override
            public void onFailure(@NonNull Call<List<Person>> call, @NonNull Throwable t) {
                apiResponse.postValue(null);
            }
        });

        return apiResponse;
    }
}

然后在viewmodel类中,我为每个新请求添加源。

public class SearchViewModel extends ViewModel {

    private MediatorLiveData<List<Person>> mApiResponse;
    private PeopleRepository mApiRepo;

    public SearchViewModel() {
        mApiResponse = new MediatorLiveData<>();
        mApiRepo = new PeopleRepository();
    }

    public LiveData<List<Person>> getPlayers() {
        return mApiResponse;
    }

    public void performSearch(String name){
        mApiResponse.addSource(mApiRepo.search(name), new Observer<List<Person>>() {
            @Override
            public void onChanged(List<Person> apiResponse)            {
                mApiResponse.setValue(apiResponse);
            }
        });
    }
}

活动

bt_search.setOnClickListener {
    val player_name = et_player.text.toString()
    viewModel.performSearch(player_name)
}

项目范围

我在一个个人项目中

目标

使用MVVM +实时数据+存储库模式

问题

我只发现了一种使用简单方法的教程:观察一个访问repository对象的LiveData对象,并且只获取一次数据。

在示例中:从Web服务获取所有人( select * from people )。

我的情况是:从Web服务中获取具有名称的人( select * from people where name=? )。

https://medium.com/@elye.project/kotlin-and-retrofit-2-tutorial-with-working-codes-333a4422a890 https://medium.com/@sriramr083/error-handling-in-retrofit2-in -mvvm-库图案a9c13c8f3995

疑惑

一个好主意是使用MediatorLiveData类来merge所有来自用户输入的请求吗?

我应该使用MutableLiveData并更改repository类并使用自定义的clousure吗?

有没有更好的方法?

我也将这种模式与MediatorLiveData一起使用,但这构成了一个问题。 从用户角度看,它似乎还可以正常工作,但是这里的一个问题是,每次调用performSearch() ,存储库都会创建一个新的LiveData对象,该对象还通过addSource()添加到MediatorLiveData中。

一个想法可能是让存储库仅创建一次MutableLiveData对象,并且在连续调用时只需更新其值即可。 因此, MutableLiveData<List<Person>> apiResponse; 将是在search()方法中初始化的未初始化的私有字段。

例如。 if (apiResponse == null) apiResponse = new MutableLiveData();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM