More FlatMap variations

There are a few more .flatMap() variations that we can use. Recall the following block:

Observable.interval(0, 5, TimeUnit.SECONDS)
.flatMap(
i -> yahooService.yqlQuery(query, env)
.toObservable()
)

The .yqlQuery() call returns a Single type and thus cannot be used directly with .flatMap(), which expects to consume the Observable type. In this case, to work around that, we had to use the following to convert it to the Observable type:

.toObservable()

However, there is a better way. Instead of using .flatMap(), we can use .flatMapSingle() to do the same thing:

Observable.interval(0, 5, TimeUnit.SECONDS)
.flatMapSingle(i -> yahooService.yqlQuery(query, env))

.flatMapSingle() takes the Single type and automatically converts it to the Observable type.

There are other similar methods like that:

  • .flatMapMaybe(): This is for Maybe types
  • .flatMapCompletable(): This is for Completable types
  • .flatMapObservable(): This is for Observable types
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.18.141