Delphian
Delphian

Reputation: 1760

RxJava2 delay doesn't work

I have Repository method:

public Maybe<String> getId(String id){
    return mApiInterface.get().getId(id)
            .subscribeOn(mSchedulerProvider.io())
            .observeOn(mSchedulerProvider.mainThread());
}

I am trying to use this method in cycle with delay:

 final ArrayList<String> ids = SharedPreferenceProvider.getIds();

    for(final String id:ids) {
        mRepository.get().getId(id)
                .delay(5, TimeUnit.SECONDS)
                .subscribeWith(new DisposableMaybeObserver<String>() {
                    @Override
                    public void onSuccess(String s) {  }

                    @Override
                    public void onError(Throwable e) { }

                    @Override
                    public void onComplete() {  }
                });
    }

But the delay method doesn't work. What am I doing wrong? Sometimes I need a delay between two requests.

Final variant of the decision:

Flowable.zip(Flowable.fromIterable(ids),
                        Flowable.interval(0, 5, TimeUnit.SECONDS).onBackpressureBuffer(),
                        new BiFunction<String, Long, String>() {
                            @Override
                            public String apply(String s, Long aLong) throws Exception {
                                return s;
                            }
                        }
                ).flatMapMaybe(new Function<String, MaybeSource<String>>() {
                    @Override
                    public MaybeSource<String> apply(String s) throws Exception {
                        return mRepository.get().testMaybe(s);
                    }
                }).subscribeWith(new DisposableSubscriber<String>() {
                    @Override
                    public void onNext(String s) {

                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

Upvotes: 1

Views: 1389

Answers (2)

akarnokd
akarnokd

Reputation: 69997

I need to send request with id every 5 seconds.

But I need - mRepository.get().getId(id1) - wait 5 sec - mRepository.get().getId(id2) - wait 5 sec - etc

You could zip with an interval, then flatMap in the repository call

Flowable.fromArray(ids) // or fromIterable(ids)
.zipWith(
    Flowable.interval(0, 5, TimeUnit.SECONDS).onBackpressureBuffer(),
    (id, time) -> id
)
.flatMapMaybe(id -> mRepository.get().getId(id), 1)
// .repeat()
.subscribe(/* ... */)

Upvotes: 3

Nguyễn Trung Hiếu
Nguyễn Trung Hiếu

Reputation: 2032

Try it!

public Maybe<String> getId(String id){
    return mApiInterface.get().getId(id)
            .delay(5, TimeUnit.SECONDS)
            .subscribeOn(mSchedulerProvider.io());
}

//And

 for(final String id:ids) {
               mRepository.get().getId(id)
                       .observeOn(mSchedulerProvider.mainThread())
                       .subscribeWith(new DisposableMaybeObserver<String>() 
{
                   @Override
                   public void onSuccess(String s) {  }

                   @Override
                   public void onError(Throwable e) { }

                   @Override
                   public void onComplete() {  }
               });
   }

Upvotes: 0

Related Questions