Android Backend Starter setFilter

谁说胖子不能爱 提交于 2019-12-04 15:52:47

You should post the error (or response) you get in the LogCat and also in the backend log. Filters are a bit tricky. I suggest you to check CloudQuery documentation to see the filter limitations, and CloudEntity to check the data you can send, retrieve and how.

About queries the documentation says:

Queries in the Cloud Backend API have some restrictions that originate from the App Engine Datastore. By default, App Engine automatically creates an index for each user defined property (with the exception of List/Map properties). This index supports the following forms of queries: - Kind name specified, no filters, no sorting - Queries with only equality filters (F.eq) on indexed properties with no sorting - Queries with only inequality filters, such as F.lt, F.ge, on a single indexed property - Queries with one sort order on one property with no filters

For example, for String properties you can only check for equality (eq/ne). You can't mix equaltiy filters with inequality ones. i.e. :

F.and(F.eq(PROPERTY_NAME, "Manuel"), F.gt(PROPERTY_AGE, Integer.valueOf(18)));

Will give you an error DatastoreNeedIndexException.

The documentation also says:

All other query forms will raise a DatastoreNeedIndexException on the backend. To use a more complex form of queries, you need to add an Index Configuration on the backend.

To do such you need edit datastore-indexes.xml in your backend. In order to do that you can use the command ./appengine-java-sdk/bin/appcfg.sh update_indexes myapp/war.

Looks promising though I haven't tested it yet...

jamachad

Filters are case-sensitive, in the Guestbook example:

  1. cq.setFilter(F.eq("MESSAGE","fabulous")); -> This won't work... don't let Google Cloud Console fool you.
  2. cq.setFilter(F.eq("message","fabulous")); -> This will work... don't know why is case sensitive.

Let me know if this was the problem.

What I have to do if I want to filter by one parameter?

I think it is better to use getCloudBackend().listByProperty() method instead of getCloudBackend().list()

Example of this method usage:

getCloudBackend().listByProperty("yourKindName", "yourPropertyName", Op.EQ,
yourPropertyValueObject, null, 1, Scope.PAST, yourHandler);

and if I want to filter by the owner?

You can use the listed above method. Just put your owner property name instead of yourPropertyName

and if I want to put more than one filter?

To use multiple filter for one property I created listByPropertyAnd() method in CloudBackendAsync class:

    public void listByPropertyAnd(String kindName, String propertyName,
            CloudQuery.Order order, int limit, Scope scope,
            CloudCallbackHandler<List<CloudEntity>> handler, F... filters) {

        CloudQuery cq = new CloudQuery(kindName);
        cq.setFilter(F.and(filters));
        cq.setSort(propertyName, order);
        cq.setLimit(limit);
        cq.setScope(scope);
        this.list(cq, handler);
    }

Example of this method usage:

getCloudBackend().listByPropertyAnd("yourKindName", "yourKindName", Order.DESC,
                1000, Scope.FUTURE_AND_PAST, yourHandler,
                F.gt("yourKindName", yourFirstValue),
                F.lt("yourKindName", yourSecondValue), F.eq("date", mToday));

In this case you can use any number of filters for one property but it should be compliant with datastore indexing rules. Also it might be useful to read this Java Datastore Filters and this one Mobile Backend Starter - API Guide

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!