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.

Why resubscribe the source observable emit same output when I use retryWhen operator?

See original GitHub issue

code:

str = "aaa";
        Observable.just(str).map(new Func1<String, String>() {
                    @Override
                    public String call(String s) {
                        Log.i("====", "s == " + s);
                        if ("aaa".equals(s)) throw new RuntimeException(s);
                        return s + "123";
                    }
                }).retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {
                    @Override
                    public Observable<?> call(Observable<? extends Throwable> observable) {
                        return observable.zipWith(Observable.range(1, 4), new Func2<Throwable, Integer, Integer>() {
                            @Override
                            public Integer call(Throwable throwable, Integer i) {
                                str = "ggg";
                                return i;
                            }
                        }).flatMap(new Func1<Integer, Observable<? extends Long>>() {
                            @Override
                            public Observable<? extends Long> call(Integer retryCount) {
                                return Observable.timer(1, TimeUnit.SECONDS);
                            }
                        });
                    }
                }).subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        Log.i("====k", "s = " + s);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        Log.i("====", "throwable = " + throwable.getMessage());
                    }
                });

In my opinion,it should log

aaa
ggg
ggg
...

but in fact it always log

aaa
aaa
aaa
...

why?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
akarnokdcommented, Nov 22, 2016

You don’t seem to understand how a value read from a variable won’t change if you change the variable. Use defer:

Observable.defer(() -> ApiClient.groupList(groupId))
   // ... the rest
0reactions
fanturbocommented, Nov 22, 2016

@akarnokd yes,I didn’t understand where differences are between Observable.just and Observable.fromCallable.Since you say ‘No’,I think retrofit’s return Observable is like Observable.fromCallable,it is not a constant Observable and it will use fresh value when retry again. But i was wrong.retrywhen operator’s meaning is resubscribe source Observable,in my code,it is constant because ApiClient.groupList(groupId)) has produced,it is like Observable.just.(I think you should say ‘Yes’ because I think retrofit’s return Observable is like a constant Observable) But when use Observable.defer,the result is different because defer operator decides the Observable is new(use fresh variable) when subscribe. Thanks for your help!love you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

RxJava's repeatWhen and retryWhen, explained
The output Observable has to use the input Observable as its source. You must react to the Observable<Throwable> and emit based on it; ......
Read more >
why does RxJS retryWhen operator completes with its inner ...
Returns an Observable that mirrors the source Observable with the exception of an error. If the source Observable calls error, ...
Read more >
Retry operator - ReactiveX
The retryWhen operator is similar to retry but decides whether or not to resubscribe to and mirror the source Observable by passing the...
Read more >
Resubscribe to an Observable on Error with RxJS retry
This is actually an Observable that only has next values. It doesn't have an error. It happens to be that these values are...
Read more >
retryWhen - Learn RxJS
import { map, tap, retryWhen, delayWhen } from 'rxjs/operators';. ​. //emit value every 1s. const source = interval(1000);. const example = source.pipe(.
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