I'm trying to use Cursors on Objectify v5 but when following the example I get an exception thrown.
My code:
import com.google.appengine.api.datastore.Cursor;
import com.google.appengine.api.datastore.QueryResultIterator;
Query<IndicadorEntity> query = ofy().load().type(IndicadorEntity.class).limit(1000);
query.startAt(Cursor.fromWebSafeString("1"));
final List<IndicadorEntity> indicadorEntities = new ArrayList<>();
QueryResultIterator<IndicadorEntity> iterator = query.iterator();
while (iterator.hasNext()) {
indicadorEntities.add(iterator.next());
}
and I'm getting the following exception:
java.lang.IllegalArgumentException: Unable to decode provided cursor.
[INFO] GCLOUD: at com.google.appengine.api.datastore.Cursor.fromWebSafeString(Cursor.java:115)
Whatis wrong? I can't find any issue like this on Google and I just followed the example here.
EDIT1: QUERY RETURNING AND EMPTY CURSOR
final IndicadorEntityCursor indicadorEntityCursor = new IndicadorEntityCursor();
Query<IndicadorEntity> query = ofy().load().type(IndicadorEntity.class)
.filter(FILTRO_CAMPANA, this.getCampannaEntity(campanna))
.filter(FILTRO_FECHA_BAJA, null)
.limit(Constantes.DATASTORE_LIMIT)
.order("titulo");
if (cursor != null) {
query = query.startAt(Cursor.fromWebSafeString(cursor));
}
indicadorEntityCursor.setIndicadores(query.list());
indicadorEntityCursor.setCursor(query.iterator().getCursor().toWebSafeString());
return this.indicadorCursorMapper.map(indicadorEntityCursor);
Thanks.
There are two things going on here. One problem is this:
query.startAt(Cursor.fromWebSafeString("1"));
Needs to be this:
query = query.startAt(Cursor.fromWebSafeString("1"));
Objectify's API is built in the functional style. The intermediate query objects are immutable; the startAt
method returns a new intermediate query object that starts at that cursor value and does not mutate state in another object.
However, that's not the cause of your exception. The cause of your exception is exactly what it says: "1" is not a cursor string. You must obtain cursor strings by getting a Cursor
object and calling toWebSafeString()
on it. Perhaps you want the query offset()
method?