question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

if I get new LiveData response from Repository everytime, how to set value to val LiveData in ViewModel without Transformations.switchMap()?

See original GitHub issue

I 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:open
  • Created 4 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

1reaction
ukyo6commented, Aug 13, 2019

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.

1reaction
HurryYUcommented, Aug 12, 2019

我也遇到了同样的问题: 如果我只想点击按钮获取数据,没有任何参数的变化,我将无法使用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 that LiveData is unique?Each time LiveData returns from Repository is a new LiveData. How to convert LiveData returned by Repository into LiveData in ViewModel monitored by Activity, and ensure that LiveData is always the same object?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use livedata with 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...
Read more >
LiveData overview
The function passed to switchMap() must return a LiveData object, as illustrated by the following example: LiveData<User> user =  ...
Read more >
Kotlin's Flow in ViewModels: it's complicated - Christophe Beyls
LiveData is a simple observable data holder class that is also lifecycle-aware. New values are only dispatched to observers when their ...
Read more >
LiveData Tutorial for Android: Deep Dive - RayWenderlich.com
LiveData is always observed inside a UI Lifecycle owner, which can be an Activity or a Fragment. You receive the latest value of...
Read more >
Android: Understand LiveData
Architecture components are common for Android developers these days. For a long time, everyone has been using one or other architecture ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found