Vladimir Jovanović
Vladimir Jovanović

Reputation: 2267

Delay onComplete() in Observable

I am doing something like this:

return Observable.zip(
    Observable.interval(0, MOVE_LENGTH_IN_MILLIS, TimeUnit.MILLISECONDS),
    Observable.fromIterable(mySuperNiceArrayList),
        new BiFunction<Long, ItemTest, ItemTest>() {
            @Override
            public ItemTest apply(@NonNull Long aLong, @NonNull ItemTest itemTest) throws Exception {
                  return itemTest;
            }
        }
)

The problem that I have is that immediately after the last item is emitted, onComplete is called. Is there a way to postpone/delay the call of onComplete method?

Hacky solution is to add one more element to ArrayList and to ignore it in onNext().

Upvotes: 3

Views: 748

Answers (1)

akarnokd
akarnokd

Reputation: 70007

Concat the entire zip with an empty Observable delayed:

return Observable.zip(
    Observable.interval(0, MOVE_LENGTH_IN_MILLIS, TimeUnit.MILLISECONDS),
    Observable.fromIterable(mySuperNiceArrayList),
        new BiFunction<Long, ItemTest, ItemTest>() {
            @Override
            public ItemTest apply(@NonNull Long aLong, 
                    @NonNull ItemTest itemTest) throws Exception {
                return itemTest;
            }
        }
).concatWith(Observable.<ItemTest>empty()
                 .delay(MOVE_LENGTH_IN_MILLIS, TimeUnit.MILLISECONDS));

Edit the delay should happen after the zip.

Upvotes: 4

Related Questions