We have shown how to fetch individual records by ids, but in real-world application there are usually needs to show a list of items according to some criteria. It is supported by queries in Skygear.
Let's see how to fetch a list of to-do items to be displayed in our hypothetical To-Do app:
We constructed a
SKYQuery to search for
todo records. There are no additional
criteria needed so we put the predicate to
nil. Then we assigned a
NSSortDescription to ask Skygear Server to sort the
todo records by
order field in ascending order.
SKYQuery with ease, we recommend using the methods provided to add constraints. However, you can also use
NSPredicate to add constraints if you wish. The following features are supported:
IN operator can be used to query a key for value that matches one of the
item in an
If the key being queried is a JSON type, the
IN operator can also be used to
query the key to check if it contains a particular value:
SKYRelationPredicate can be used to query for records having a relation with
the current user. For this kind of query, the record have an relation with
the current user if the record has an attribute that contains a user having
the relation with the current user.
For example, to query for records owned by a user that the current user is following:
SKYKit query supports filtering records by the reference field of records:
You can query by fields on a referenced record. Following the above example, if we want to narrow orders placed in Italian restaurants only:
We can sort records returned by:
NSPredicate to apply filtering on query results. You can use other parameters to sort your queries.
We can limit the numbers of records returned by:
We can also set an offset number to the query by:
offset number means skipping that many rolls before beginning to return rows. If the
offset number is 0, then no rows will be skipped. If you use both
offset numbers of rows will be skipped before starting to limit the number of rows returned.
Now the first 5 records in the result list are skipped. The query result starts with the 6th record. It works just like SQL offset.
To get the number of all records matching a query, set the property
overallCount property of
YES. The record count can be
overallCount property of
perRecordCompletionBlock is first called.
Skygear support eager loading of referenced records when you are querying the
referencing records. It's done by supplying a key path expression to
It is possible to eager load records from multiple keys, but doing so will impair performance.
ON DELETE CASCADE TBC.