Architecture-components-samples: AddSource not work - On Change not call

Created on 17 Nov 2017  Â·  13Comments  Â·  Source: android/architecture-components-samples

Inside NetworkBoundResource when I add the dbSource to the result the 'onChange' method not call
Someone had the same problem? I try to figure it out for 3 days.
in other places it work and the code is the same like in the GitHub.

Most helpful comment

MediatorLiveData -> addSource() not call if you don't observe the result.
Look into addSource() method
if (hasActiveObservers()) {
e.plug();
}

All 13 comments

I have the same situation,in my case:the first time open the application without the network,then connect network and retry Repository access data method. 'loadFromDb' can not call the dbSource onChanged.

But when i am use ‘Transformations.switchMap()’ like
GithubBrowserSample Code, it can call the dbSource onChanged.

My WrongCode is direct use 'moviesRepository.getMovies(moveType)'.

My CorrectCode:

  LiveData<Resource<List<Movie>>> mObservableMovies;
  MutableLiveData<String> moveType = new MutableLiveData<>();
  mObservableMovies = Transformations.switchMap(moveType, new Function<String, LiveData<Resource<List<Movie>>>>() {
        @Override
        public LiveData<Resource<List<Movie>>> apply(String input) {
            if (input == null) {
                return AbsentLiveData.create();
            } else {
                return moviesRepository.getMovies(input);
            }
        }
     });

I hope my experience can help you.

It's not help. and also I don't understand why this can be the problem.
it's very strange only from NetworkBound the onChange not call.

MediatorLiveData -> addSource() not call if you don't observe the result.
Look into addSource() method
if (hasActiveObservers()) {
e.plug();
}

As @namdp say you must result LiveData so addSource() called

How do you solve the problem?it refuse me too! as @abbenyyyyyy say,I solve it,thanks very much

@namdp thanks you oh

What should I do if I don't have any params, like in @abbenyyyyyy's answer? If I load data without any queries or paths?

You have to observe your LiveData from Activity/Fragment, if not
observe -> AddSource
not work

On Tue, Aug 14, 2018 at 4:58 PM, Alex Marinovskiy notifications@github.com
wrote:

What should I do if I don't have any params, like in @abbenyyyyyy
https://github.com/abbenyyyyyy's answer? If I load data without any
queries or paths?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/googlesamples/android-architecture-components/issues/235#issuecomment-412819410,
or mute the thread
https://github.com/notifications/unsubscribe-auth/APWOs9B-mb7kMYxKEoLK2PZF2-zgpIaZks5uQp88gaJpZM4QhRxT
.

@namdp could you, please, provide some example?

I've found a workaround - use MediatorLiveData inside ViewModel and every time call addSource(repository.getData()). Transformations in GitHubBrowserSample work because inside methods map() and switchMap() MediatorLiveData is using. But, is my solution ok? Or is there another, better way?

And why result in NetworkBoundResource doesn't trigger? It has no observers, I know, but, what happened with it when I try to reload data (e.g. Retry if there was no internet connection and db was empty)?

No, keep your LiveData in ViewModel, but you must observe it if you want
AddSource trigger
Check out google sample,
https://github.com/googlesamples/android-architecture-components/tree/master/GithubBrowserSample

  • Ex: In "SearchViewModel" -> val results: LiveData>>
    you can see the results was observed in "SearchFragment"

searchViewModel.results.observe(...)

On Thu, Aug 16, 2018 at 2:05 AM, Alex Marinovskiy notifications@github.com
wrote:

@namdp https://github.com/namdp could you, please, provide some example?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/googlesamples/android-architecture-components/issues/235#issuecomment-413301825,
or mute the thread
https://github.com/notifications/unsubscribe-auth/APWOsxbWWw310ZPB9Hags5Ywf3Fa7vLKks5uRHEJgaJpZM4QhRxT
.

@namdp so, the point in using Transformations (or MediatorLiveData if I have no queries or paths for loading data), right?

Like this?

organizationsMediatorLiveData.addSource(organizationsLiveData) {
organizationsMediatorLiveData.value = it
}

And then observe organizationsMediatorLiveData?

Also, I've found this solution: https://stackoverflow.com/questions/46627624/observing-viewmodel-for-the-second-time-returns-null-in-android

How do you think, is it ok?

This doesn't seem to be an issue with the samples. You can file an issue against Lifecycle on the issue tracker if you're still having an issue.

thank you it is running very well

Was this page helpful?
0 / 5 - 0 ratings