Objectify return List & Cursor

I'm trying to use the cursor with Objectify and the Google App Engine to return a subset of the data and the cursor so that I can get more data when the user is ready. I found an example here that looks exactly the way I need it, but I don't know how to return the final list plus cursor. Here is the code I have:

@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); } 

Note that this was changed from the previous endpoint at which I was returning CollectionResponse for ALL data. My data set is large enough that it is no longer practical. Basically, I don’t know what was in the user function 'serial (tClass, result, encodeCursor), which allows it to be returned to the user.

Here is another example, but it also does not answer my question.

+5
source share
2 answers

I don’t quite understand what you are asking, but I see one error in your code:

 query.startAt(Cursor.fromWebSafeString(cursorStr)); 

... should be:

 query = query.startAt(Cursor.fromWebSafeString(cursorStr)); 

Objectify objects are immutable, functional objects.

+5
source

After a long failure, I realized that there is a cursor in CollectionResponse :(

Here is the complete code that I used, including the comment from the image above:

  @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; } 
+4
source

Source: https://habr.com/ru/post/1200375/


All Articles