【问题标题】:LiveData, MVVM and Repository PatternLiveData、MVVM 和存储库模式
【发布时间】:2019-03-02 01:43:20
【问题描述】:

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

我正在使用 MediatorLiveData 类,因为它似乎对更新 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;
    }
}

然后在视图模型类中,我为每个新请求添加源代码。

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-repository-pattern-a9c13c8f3995

怀疑

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

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

有没有更好的方法?

【问题讨论】:

    标签: android mvvm kotlin repository-pattern android-livedata


    【解决方案1】:

    我也将这种模式与 MediatorLiveData 一起使用,但它形成了一个问题。 从用户的角度来看,它似乎运行得很好,但这里的一个问题是,每次调用 performSearch() 时,存储库都会创建一个新的 LiveData 对象,该对象还会通过 addSource() 添加到 MediatorLiveData。

    一个想法可能是让存储库只创建一次 MutableLiveData 对象,并在连续调用时更新它的值。所以MutableLiveData&lt;List&lt;Person&gt;&gt; apiResponse; 将是一个未初始化的私有字段,在 search() 方法中被初始化。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-07
      • 2020-09-17
      • 2020-03-16
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多