Filtering API

Mk5 edited this page Apr 5, 2018 · 3 revisions

Note: filtering does not work at GWT yet. It will be covered in next release.

Data filtering can be achieved by following methods:

  • GdxFIRDatabase#orderBy
  • GdxFIRDatabase#filter

Order by

You have following order-by modes to work with:

  • OrderByMode.ORDER_BY_CHILD
  • OrderByMode.ORDER_BY_KEY
  • OrderByMode.ORDER_BY_VALUE

These modes are equivalents of firebase order by methods. The second argument of the orderBy method is optional so it can be null. It is useful only for ORDER_BY_CHILD mode for specifying the child name.

Filtering

You have following filter types to work with:

  • FilterType.LIMIT_FIRST
  • FilterType.LIMIT_LAST
  • FilterType.START_AT
  • FilterType.END_AT
  • FilterType.EQUAL_TO

These types are equivalents of firebase limiting/filtering methods. In contrast to the order-by, you can combine multiple filters with one query. The types: START_AT, END_AT, and EQUAL_TO are dependent on current order-by mode. You can read more about it in firebase sdk docs or take a look at following examples.

Limitation

  • A filter method second argument should be one of the following types: String, Double, Boolean otherwise exception will be thrown.
  • You can use filtering API only when you try to fetch List otherwise the exception will be thrown.
  • Remember that you have to use only one order by clause for one query.

Examples

Limiting items

GdxFIRDatabase.instance().inReference("/items")
    .filter(FilterType.LIMIT_FIRST, 2)
    .readValue(List.class, new DataCallback<List<Item>>(){
        //...
    });

Get docs which have key between 2 and 3

GdxFIRDatabase.instance().inReference("/items")
    .orderBy(OrderByMode.ORDER_BY_KEY, null)
    .filter(FilterType.START_AT, "2")
    .filter(FilterType.END_AT, "3")
    .readValue(List.class, new DataCallback<List<Item>>(){
        // ...
    });

Ordering by POJO field

GdxFIRDatabase.instance().inReference("/items")
    .orderBy(OrderByMode.ORDER_BY_CHILD, "some_field")
    .onDataChange(List.class, new DataChangeListener<List<Item>>(){
        // ...
    });
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.