如何正确地使用Livedata和Transformations.switchMap来获取初始数据? [英] How to use livedata with Transformations.switchMap correctly to get initial data?

查看:2365
本文介绍了如何正确地使用Livedata和Transformations.switchMap来获取初始数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我开始第一次使用LiveData.首先,我将所有代码放入包括要在服务器中开始搜索的代码在内的viewModel中. 我这样使用LiveData:

right now I am starting to use LiveData for the first time. First I put all of my code in the viewModel including the code to start a search in the server. I used LiveData like this:

onViewCreated()片段

Fragment onViewCreated()

        viewModel.changeNotifierContacts.observe(this, androidx.lifecycle.Observer { value -> value?.let {
        recyclerViewAdapter.setData(value)
    } })

这按预期工作.现在,我按照MVVM模式添加一个存储库层. (为此,我将联系人搜索功能移至了存储库类) 首先,我实现了ViewModel和存储库之间的连接,如下所示:

This was working as expected. Now I add a repository layer following MVVM pattern. (For this I moved my contact search functionality to repository class) First I implemented the connection between ViewModel and repository like this:

ViewModel代码:

ViewModel code:

fun getContacts(): MutableLiveData<ContactGroup> {
   return contactSearchRepository.changeNotifierContacts;
}

fun search(newSearchInput: String) {
   contactSearchRepository.searchInRepository(newSearchInput)
}

现在,我读到这篇文章,告诉我们不要像这样使用LiveData: https://developer.android.com/topic/libraries/architecture/livedata#merge_livedata

Now I read this article that told us to not use LiveData like this: https://developer.android.com/topic/libraries/architecture/livedata#merge_livedata

此页面上的示例:

class MyViewModel(private val repository: PostalCodeRepository) : ViewModel() {

private fun getPostalCode(address: String): LiveData<String> {
    // DON'T DO THIS
    return repository.getPostCode(address)
}

}

相反,我们应该使用类似这样的东西:

Instead we should use something like this:

var changeNotifierContacts : LiveData<ContactGroup> = Transformations.switchMap(searchInput) {
    address -> contactSearchRepository.getPostCode(address) }

问题:

  1. 我是否正确理解了本文,还是可以使用第一个实现?
  2. 在viewModel的构造函数中,我正在创建存储库对象的实例,该实例开始观察服务器数据并获取初始数据. (例如,我正在获取所有朋友的列表).如果使用第一个实现,我将获得这些初始数据.如果我正在使用Transformations.switchMap实现,则不会获得此初始数据.我首先必须在这里开始搜索以获取更新的数据.这不是我想要的,我还需要不进行搜索就显示我的朋友"列表.
  3. 我可以在这里使用另一种方法吗?也许LiveData不是将ViewModel与存储库连接的最佳解决方案?

感谢您的回应和建议!

推荐答案

我是否正确理解本文,还是可以使用第一个实现?

Did I understand this article correctly or can I use my first implementation?

我认为您做到了,但是我相信您已经扩展了这个概念.

I think you did, but I believe you have expanded the concept too much.

如果您希望用户输入搜索内容来获得答案,则应该像他们说的那样做:

If you are expecting the user to enter a search to receive an answer, you should do like they said:

class MyViewModel(private val repository: PostalCodeRepository) : ViewModel() {
    private val addressInput = MutableLiveData<String>()
    val postalCode: LiveData<String> = Transformations.switchMap(addressInput) {
            address -> repository.getPostCode(address) }


    fun setInput(address: String) {
        addressInput.value = address
    }
}

但是,如果要加载默认列表,则应像在第一个示例中那样进行操作:

However, if you are loading a default list, you should do it like you did in your first example:

val getContact = contactSearchRepository.changeNotifierContacts

在这种情况下,您将必须遵守 getContact postalCode .

In this case you will have to observe getContact and postalCode.

在viewModel的构造函数中,我正在创建存储库对象的实例,该实例开始观察服务器数据并获取初始数据. (例如,我正在获取所有朋友的列表).如果使用第一个实现,我将获得这些初始数据.如果我正在使用Transformations.switchMap实现,则不会获得此初始数据.我首先必须在这里开始搜索以获取更新的数据.这不是我想要的,我还需要不进行搜索就显示我的朋友"列表.

In my constructor of the viewModel I am creating an instance of my repository object that is starting to observe server data and it is getting initial data. (For example I am getting a list of all my friends). I am getting this initial data if I am using my first implementation. If I am using Transformations.switchMap implementation I am not getting this initial data. I first have to start a search here to get updated data then. This is not what I want, I also need to display "my friends" list without doing a search.

您可以通过默认搜索开始片段/活动,例如:

You can start your fragment/activity with a default search, like this:

MyViewModel.setInput("Friends")

这样,您无需观察两个对象,因为 postalCode 将提供所有答案.

This way you do not need to observe two objects as postalCode will provide all answers.

我可以在这里使用另一种方法吗?也许LiveData不是将ViewModel与存储库连接的最佳解决方案?

Is there another approach I can use here? Maybe LiveData is not the best solution to connect ViewModel with Repository?

我认为实时数据是您的答案.学习曲线完成后,处理起来就更容易了!

I think live data is your answer. After done with the learning curve it becomes easier to deal with!

希望对您有帮助!

这篇关于如何正确地使用Livedata和Transformations.switchMap来获取初始数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆