Basic Queries 基本
用callback來處理 結果
//ParseException <==有時不見得 失敗 ,它有定義一些奇怪的例外。
setSkip
. 跳幾個結果
排序
可以增加排序規則
可以設條件式:
可以設多個條件
whereDoesNotExist
.
可以將ParseQuery 當成參數傳入
還可以用 regular expression 在
whereMatches
:
Parse 使用 Perl-compatible regular expression 格式. Since the regular expression string is escaped by Java before being interpreted by the regular expression engine, be sure that you replace backslashes for the regular expression with double backslashes.
whereMatches
has an overload which supports many standard PCRE modifier characters:i
- Case insensitive searchm
- Search across multiple lines of input
There are also several helper functions for standard string operations: substrings, prefixes, and suffixes. Use
whereContains
to restrict to string values containing a substring:
Use
whereStartsWith
to restrict to string values that start with a particular string. Similar to a MySQL LIKE operator, this is indexed so it is efficient for large datasets:Relational Queries
There are several ways to issue queries for relational data. If you want to retrieve objects where a field matches a particular
ParseObject
, you can use whereEqualTo
just like for other data types. For example, if each Comment
has a Post
object in its post
field, you can fetch comments for a particular Post
:
If you want to retrieve objects where a field contains a
ParseObject
that matches a different query, you can use whereMatchesQuery
. Note that the default limit of 100 and maximum limit of 1000 apply to the inner query as well, so with large data sets you may need to construct queries carefully to get the desired behavior. In order to find comments for posts containing images, you can do:
If you want to retrieve objects where a field contains a
ParseObject
that does not match a different query, you can use whereDoesNotMatchQuery
. In order to find comments for posts without images, you can do:
In some situations, you want to return multiple types of related objects in one query. You can do this with the
include
method. For example, let's say you are retrieving the last ten comments, and you want to retrieve their related posts at the same time:
You can also do multi level includes using dot notation. If you wanted to include the post for a comment and the post's author as well you can do:
You can issue a query with multiple fields included by calling
include
multiple times. This functionality also works with ParseQuery helpers like getFirst()
andgetInBackground()
.Caching Queries 暫存收尋結果
It's often useful to cache the result of a query on disk. This lets you show data when the user's device is offline, or when the app has just started and network requests have not yet had time to complete. Parse takes care of automatically flushing the cache when it takes up too much space.
The default query behavior doesn't use the cache, but you can enable caching with
setCachePolicy
. For example, to try the network and then fall back to cached data if the network is not available:
Parse provides several different cache policies:
IGNORE_CACHE
The query does not load from the cache or save results to the cache.IGNORE_CACHE
is the default cache policy.CACHE_ONLY
The query only loads from the cache, ignoring the network. If there are no cached results, that causes aParseException
.NETWORK_ONLY
The query does not load from the cache, but it will save results to the cache.CACHE_ELSE_NETWORK
The query first tries to load from the cache, but if that fails, it loads results from the network. If neither cache nor network succeed, there is aParseException
.NETWORK_ELSE_CACHE
The query first tries to load from the network, but if that fails, it loads results from the cache. If neither network nor cache succeed, there is aParseException
.CACHE_THEN_NETWORK
The query first loads from the cache, then loads from the network. In this case, theFindCallback
will actually be called twice - first with the cached results, then with the network results. This cache policy can only be used asynchronously withfindInBackground
.
If you need to control the cache's behavior, you can use methods provided in ParseQuery to interact with the cache. You can do the following operations on the cache:
- Check to see if there is a cached result for the query with:
- Remove any cached results for a query with:
- Remove cached results for all queries with:
- Control the maximum age of a cached result with:
Query caching also works with ParseQuery helpers including
getFirst()
and getInBackground()
.Counting Objects
If you just need to count how many objects match a query, but you do not need to retrieve all the objects that match, you can use
count
instead of find
. For example, to count how many games have been played by a particular player:
If you want to block the calling thread, you can also use the synchronous
query.count()
method.Compound Queries
If you want to find objects that match one of several queries, you can use
ParseQuery.or
method to construct a query that is an or of the queries passed in. For instance if you want to find players who either have a lot of wins or a few wins, you can do:
You can also add more constraints to the newly constructed
ParseQuery
which act as an 'and' operator.
沒有留言:
張貼留言