1

我编写了代码来从 Couchbase 视图中获取异步数据:

这是实现类

AsyncViewResult viewResult = 
    offerCouchDao.findViewResultAsync(DDN, VIEWNAME).toBlocking().single();
if (viewResult.success()) {
    Observable<JsonArray> JAoffer =
        offerCouchDao.getAsyncJsonObject(viewResult.rows());
    Object response = offerCouchDao.getData(JAoffer);
    System.out.println("Data is "+response);
        return new OfferResponse(true,"Offer Data",response).toJson();
}

这是 OfferCouchDao:

public Observable<AsyncViewResult> findViewResultAsync(String ddn, String viewname) {
    ViewQuery allResult = ViewQuery.from(ddn, viewname);
    return couchbaseManager.getMobikwikBucket().async().query(allResult);
}

public Observable<JsonArray> getAsyncJsonObject(Observable<AsyncViewRow> viewResult) {
    return viewResult.
            //extract the document from the row and carve a result object using its content and id
                    flatMap(new Func1<AsyncViewRow, Observable<JsonObject>>() {
                @Override
                public Observable<JsonObject> call(AsyncViewRow row) {
                    return row.document().map(new Func1<JsonDocument, JsonObject>() {
                        @Override
                        public JsonObject call(JsonDocument jsonDocument) {
                            return JsonObject.create()
                                    .put("id", jsonDocument.id())
                                    ;
                        }
                    })
                            ;
                }
            }).filter(new Func1<JsonObject, Boolean>() {
        @Override
        public Boolean call(JsonObject jsonObject) {
            String name = jsonObject.getString("name");
            return name != null ;
        }
    })


            .collect(new Func0<JsonArray>() { //this creates the array (once)
                @Override
                public JsonArray call() {
                    return JsonArray.empty();
                }
            }, new Action2<JsonArray, JsonObject>() { //this populates the array (each item)
                @Override
                public void call(JsonArray objects, JsonObject jsonObject) {
                    objects.add(jsonObject);
                }
            });
}

public Object getData(Observable<JsonArray> jsonArraay) {
    return jsonArraay
    .map(new Func1<JsonArray, JsonArray>() {
        @Override
        public JsonArray call(JsonArray objects) {
            return objects;
        }
    })
            .onErrorReturn(new Func1<Throwable, JsonArray>() {
                @Override
                public JsonArray call(Throwable throwable) {
                    return null;
                }
            })
            .toBlocking().single();
}

我遇到的问题是返回的数据为空

以下是日志:

Data is []
Data is null

同样通过同步调用进行时:

    else {
        JsonArray keys = JsonArray.create();
        Iterator<ViewRow> iter = viewResult.rows();
        while (iter.hasNext()) {
            ViewRow row = iter.next();
            JsonObject beer = JsonObject.create();
            beer.put("name", row.key());
            beer.put("id", row.id());
            keys.add(beer);
       }
    }

我得到了预期的回应。

有人能帮我吗?

4

0 回答 0