How to emit multiple value in one Observable

Viewed 68

I am trying to download file with RxAndroid and Retrofit, I want my Observable can emit download status on processing.

My expectation:

    Observable.create()
              .emit(PENDING)
              .emit(START)
              .flatMap(() -> {
                  emit(DOWNLOADING);
                  return apiService.download();
              })
              .onSuccess(() -> {
                  emit(SUCCESS);
              )
              .onError(() -> {
                  emit(ERROR);
              })

2 Answers

You can check the download status with the desired time interval and update the status:

getDownloadStatus()
            .zipWith(Observable.interval(500, TimeUnit.MILLISECONDS), (status, interval) -> (String) status)
            .repeat()
            .distinctUntilChanged()
            .doOnNext(status -> ...). //some other operators

Use

Observable.create (emitter ->  {})
By emitter.emit(value) 

you can do it. But remember free the observable when you've done the task

Related