kaushikgopal / RxJava-Android-Samples

Learning RxJava for Android by example
Apache License 2.0
7.55k stars 1.37k forks source link

networkObserver get Exception so the final result is onError in your example when network fail even if localObeserver has data #81

Closed shakil807g closed 7 years ago

shakil807g commented 7 years ago

networkObserver().publish( network ->// Observable.merge(network,// localObserver().takeUntil(network)));

kaushikgopal commented 7 years ago

interesting.are you pointing to the same issue as this -> https://www.reddit.com/r/androiddev/comments/5d8gn8/learning_rx_by_example/da385oz/ ?

if yes, then i could potentially change the code to handle via onErrorResumeNext etc. but i also am interested in keeping the example to a bare minimum vs. a comprehensive one which handles multiple edge cases.

hmm.. but not sure if this is important enough to be a legit case vs an edge case

shakil807g commented 7 years ago

hmm..right but it would be helpful if you provide an example using onErrorResumeNext for that edge case because in my case i am getting Exception very often get from networkObserver()

kaushikgopal commented 7 years ago

the code snippet would look something like this:

private Observable<Contributor> getFreshNetworkData() {
        String githubToken = getResources().getString(R.string.github_oauth_token);
        GithubApi githubService = GithubService.createGithubService(githubToken);

        return githubService
              .contributors("square", "retrofit")
              .onErrorReturn(throwable -> {
                  Timber.e(throwable, "Error encountered getting fresh network data");
                  return Collections.emptyList();
              })
              .flatMap(Observable::fromIterable)
              .doOnSubscribe((data) -> new Handler(Looper.getMainLooper()).post(() -> adapterSubscriptionInfo.add(
                    "(network) subscribed")))
              .doOnComplete(() -> new Handler(Looper.getMainLooper()).post(() -> adapterSubscriptionInfo.add(
                    "(network) completed")));
    }

leaving the code snippet here for folks who are interested in knowing how to address this.

still want to keep the example clean, cause adding it to the actualy example makes it look like a mini app, which can deter the learning.

closing this for now with above snippet. feel free to reopen if you feel something else needs to be done.