I.S
I.S

Reputation: 2053

How map Observable object to Flowable<?> ?

 private Observable< SimpleResource > resource;
 return resource.map(new Function<SimpleResource, Flowable<Data>>() {
  @Override
  public Flowable< Data > apply(SimpleResource resource) throws Exception {
    return resource.data().toFlowable();
  }
});


  Single<Data> data();

I need to have Flowable but my result is Observable>

Upvotes: 1

Views: 1901

Answers (2)

Jon
Jon

Reputation: 1763

Since you mentioned that data() returns a Single, you need to transform all of the single streams into one large stream. To transform streams into streams, we generally use the flatmap operator:

resource.flatMapSingle(
            new Function<SimpleResource, Single<Data>>() {
                @Override
                public Single<Data> apply(SimpleResource resource) throws Exception {
                    return resource.data();
                }
            }
    ).toFlowable(BackpressureStrategy.BUFFER);

Upvotes: 2

jujka
jujka

Reputation: 1217

What you are doing wrong is applying .toFlowable at not the right spot.

Observable.fromCallable { 1 }.map {
    it * 2
}.toFlowable(BackpressureStrategy.BUFFER)

If you have different data type returned by data (sorry for Kotlin, but the concept is the same)

data class A(
        val data: Single<Int>
) {
    constructor() : this(data = Single.fromCallable { 1 })
}


val result: Flowable<Int> = Flowable
        .fromCallable {
            A()
        }
        .flatMap {
            it.data.toFlowable()
        }

Upvotes: 1

Related Questions