How to use toBlocking() in getting data and print it out


#1

Hi, here is a simple code come from couchbase tutorial JDK2.1
My jdk is 1.8 and I have include couchbase-core-io-1.1.4.jar, couchbase-java-client-2.1.4.jar, rxjava-1.0.4.jar

Observable
.just(“doc-1”, “doc-2”, “doc-3”)
.flatMap(bucket::get)
.subscribe(document -> System.out.println("Got: " + document));

But it can not compile with error message: Type mismatch: cannot convert from Observable to

And I fix it to

Observable
.just(“doc-1”, “doc-2”, “doc-3”)
.flatMap(bucket.async()::get)
.subscribe(document -> System.out.println("Got: " + document));

It works but it’s not blocked until it get the data. That is, the cluster closed before it got the data
How to use toBlocking() in getting data and print it out?

I have tried it below but fail with compilation

Observable
.just(“doc-1”, “doc-2”, “doc-3”)
.flatMap(bucket.async()::get)
.subscribe(document -> System.out.println("Got: " + document));
.toBlocking()
.single();


#2

Hi, here you want to block until you get the last document. So this should do the trick:

Observable
    .just("01", "02", "03")
    .flatMap(bucket::get)
    .doOnNext(document -> System.out.println("Got: " + document))
    .toBlocking().last();

Note that you don’t have to use the subscribe operator all the time, there are plenty of others Rx operators: http://reactivex.io/documentation/operators.html