RxJava 將一組資料每隔一定時間傳送出來

pdog發表於2018-01-26
data = [0,1,2,3.....]

    delay(time)   delay(time)    delay(time)
 0 -----------> 1 -----------> 2 -----------> 3 ...
複製程式碼

使用doOnNext() + sleep(2000)

  Flowable.fromIterable(this.mock.messages)
                .doOnNext(message -> SystemClock.sleep(2000))
                .subscribe(message -> Timber.d("message.content = %s", message.content));
複製程式碼

使用Zip操作符

Observable<Message> listObservable = Observable.fromIterable(mock.messages);
Observable<Long> timeObservable = Observable.interval(300, TimeUnit.MILLISECONDS);
Observable<String> zip =
        Observable.zip(listObservable, timeObservable, (message, aLong) -> message.content);

zip.doOnComplete(() -> Timber.d("complete"))
        .subscribe(s -> Timber.d("s = %s", s));
複製程式碼

相關文章