开发者

Objectify/AppEngine: best way to count # of objects returned by a query?

What would be the best (i.e. most efficient) way of counting the number of objects returned by a query, w/o actually loading them, using Objectify on AppEngine? I'm guessing the best is to 开发者_StackOverflow中文版fetch all the keys and counting the result:

public int getEntityCount(Long v) {
    Objectify ofy = ObjectifyService.begin();
    Iterable<Key<MyEntity>> list = ofy.query(MyEntity.class)
            .filter("field", v).fetchKeys();
    int n = 0;
    for (Key<MyEntity> e : list)
        n++;
    return n;
}

Doesn't seems to have any dedicated method for doing that. Any ideas?


Found it:

int n = Iterable<Key<MyEntity>> list = ofy().query(MyEntity.class)
      .filter("field", v).count();

It's that simple, though efficient because it retrieves all the keys. It's better to design your UI to handle unknown numbers of results (such as Google which gives clues to the number of pages but not the actual number)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜