How to get Couchbase objects in generic Observable


AsyncBucket returns Observable and Observable. How can one get to the RawJsonDocument and BinaryDocument from this Observable object?


hi @racarlson,
The Observable is an asynchronous stream. In the case of the get methods I think you are referring to, there will only be one item emitted.
The idea is that you have to subscribe to the Observable<Document> to describe what you intend to do with each item (in present case the unique item). This is invoked asynchronously, as the data becomes available, and the handling should also be asynchronous-friendly.

Have a look at the doc section on RxJava here.

Note that if you want blocking behavior or don’t want to deal with Observables for now, you’re probably better off using Bucket methods instead of AsyncBucket ones.

Here is an example of retrieving documents asynchronously:

//bucket is a sync Bucket
bucket.async().get("someKey", BinaryDocument.class)
            .subscribe(new Action1<BinaryDocument>() {
                public void call(BinaryDocument binaryDocument) {
                    try {
                        //convert the binary to some meaningful data, here a String
                        String data = binaryDocument.content()
                        //do something async, eg. update an UI via a Server Sent Event
                    } finally {
                        //BinaryDocument holds a ByteBuf as content,
                        //  which needs to be released explicitely by the user
1 Like