Objectify返回List&Cursor

eas*_*ese 5 google-app-engine objectify google-cloud-endpoints

我正在尝试使用带有Objectify和Google App Engine的光标来返回数据和光标的子集,这样我就可以在用户准备好时检索更多数据.我在这里找到了一个看起来完全像我需要的例子,但我不知道如何返回最终列表和光标.这是我的代码:

@ApiMethod(name = "listIconThemeCursor") //https://code.google.com/p/objectify-appengine/wiki/Queries#Cursors
public CollectionResponse<IconTheme> listIconThemeCursor(@Named("cursor") String cursorStr) {
    Query<IconTheme> query = ofy().load().type(IconTheme.class).limit(10);
    if (cursorStr != null ) {
        query.startAt(Cursor.fromWebSafeString(cursorStr));
    }
    List<IconTheme> result = new ArrayList<IconTheme>();
    int count = 0;
    QueryResultIterator<IconTheme> iterator = query.iterator();
    while (iterator.hasNext()) {
        IconTheme theme = iterator.next();
        result.add(theme);
        count++;
    }
    Cursor cursor = iterator.getCursor();
    String encodeCursor = cursor.toWebSafeString();
    return serial(tClass, result, encodeCursor);
}
Run Code Online (Sandbox Code Playgroud)

请注意,这是从先前的端点修改的,在该端点中我返回了所有数据的CollectionResponse.我的数据集足够大,不再实用.基本上,我不知道用户的'serial(tClass,result,encodeCursor)函数是什么让它返回给用户.

这里有另一个例子,但似乎也没有回答我的问题.

sti*_*ure 5

我不太明白你在问什么,但我在你的代码中看到一个直接的错误:

query.startAt(Cursor.fromWebSafeString(cursorStr));
Run Code Online (Sandbox Code Playgroud)

...应该:

query = query.startAt(Cursor.fromWebSafeString(cursorStr));
Run Code Online (Sandbox Code Playgroud)

Objectify命令对象是不可变的功能对象.


eas*_*ese 5

经过长时间的跋涉,我发现CollectionResponse有光标在其中:(

以下是我使用的完整代码,其中包含了上面的stickfigure的注释:

   @ApiMethod(name = "listIconThemeCursor", path="get_cursor") 
    public CollectionResponse<IconTheme> listIconThemeCursor(@Named("cursor") String cursorStr) {
        Query<IconTheme> query = ofy().load().type(IconTheme.class)
                .filter("errors <", 10)
                .limit(10);
        if (cursorStr != null ) {
            query = query.startAt(Cursor.fromWebSafeString(cursorStr));
        }
        List<IconTheme> result = new ArrayList<IconTheme>();
        QueryResultIterator<IconTheme> iterator = query.iterator();
        while (iterator.hasNext()) {
            IconTheme theme = iterator.next();
            result.add(theme);
        }
        Cursor cursor = iterator.getCursor();
        CollectionResponse<IconTheme> response = CollectionResponse.<IconTheme> builder()
                .setItems(result)
                .setNextPageToken(cursor.toWebSafeString())
                .build();

        return response;
    }
Run Code Online (Sandbox Code Playgroud)