if I get new LiveData response from Repository everytime, how to set value to val LiveData in ViewModel without Transformations.switchMap()?
See original GitHub issueI have a question.
in browseSample we always Transformations.switchMap()
to observe one input LiveData and set the value of repoRepository.search(search)
to results. As below:
val results: LiveData<Resource<List<Repo>>> = Transformations
.switchMap(_query) { search ->
if (search.isNullOrBlank()) {
AbsentLiveData.create()
} else {
repoRepository.search(search)
}
}
but in my case I just click button to get the new results by repoRepository.search(search)
every time, this means I don’t have param _query
to observe. how can I do?
I already defined results
as val, so I can’t make results = repoRepository.search(search)
in ViewModel everyTime.
If I change the repoRepository return type from LiveData<Result> to Result, then I can use results.setValue(result) manually in ViewModel. but is that right to return non-LiveData reuslt from Repository?
I’m very confused about that… in many cases we don’t have input params to Observe so we can’t use Transformations.switchMap()
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:5
Top GitHub Comments
16days past, it seems nobody care this question . if I find the best solution whick can point the val LiveData in ViewModel to the new LiveData from repository, I’ll tell you.
我也遇到了同样的问题: 如果我只想点击按钮获取数据,没有任何参数的变化,我将无法使用
Transformations .switchMap
,那么如何保证LiveData
的唯一?每次从Repository
返回的都是一个新的LiveData
,如何将Repository
每次返回的LiveData
转换为ViewModel
中被Activity
监听的LiveData
,并且保证LiveData
对象永远是同一个对象呢?如果你找到了解决方案,希望分享一下,谢谢!I had the same problem: If I only want to click on the button to get the data, without any parameter changes, I will not be able to use
Transformations .switchMap
, so how to ensure thatLiveData
is unique?Each timeLiveData
returns fromRepository
is a newLiveData
. How to convertLiveData
returned byRepository
intoLiveData
inViewModel
monitored byActivity
, and ensure thatLiveData
is always the same object?