quarks
quarks

Reputation: 35276

Retrieving Cursors from the GAE Datastore

I have a question on how Datastore generate cursors, I have this code below and even if the result list is empty a cursor is still returned:

    if(asList){
        if(startCursor != null && startCursor.getWebSafeString() != null){
            fetchOptions.startCursor(Cursor.fromWebSafeString(startCursor.getWebSafeString()));
            res = pq.asQueryResultList(fetchOptions);
        } else if(startCursor != null && startCursor.getWebSafeString() == null) {
            res = pq.asQueryResultList(fetchOptions);
        } else {
            res = pq.asList(fetchOptions);
        }
    } else {
        if(startCursor != null && startCursor.getWebSafeString() != null){
            fetchOptions.startCursor(Cursor.fromWebSafeString(startCursor.getWebSafeString()));
            res = pq.asQueryResultIterable(fetchOptions);
        } else if(startCursor != null && startCursor.getWebSafeString() == null){
            res = pq.asQueryResultIterable(fetchOptions);
        } else {
            res = pq.asIterator(fetchOptions);
        }
    }
    return res;

res here is a Query result:

String newCursor = res.getCursor().toWebSafeString();

Even if res list is empty a cursor is returned, it is normal? Or something is wrong with this?

Upvotes: 0

Views: 105

Answers (2)

Tom
Tom

Reputation: 1603

Having these "last position" cursors can be really useful for progressive handling of new data.

As in, if you persist the cursor somewhere, you can poll Datastore for new records every so often - and it's cheap because cursors mean Datastore won't scan rows.

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599490

You always get a cursor, because the datastore doesn't know or care if there are any more results. What you should do is check that the cursor actually returns something, and if not don't show the link for more results.

Upvotes: 1

Related Questions