public class ParseQuery<T extends ParseObject>
extends java.lang.Object
ParseQuery class defines a query that is used to fetch ParseObjects. The most
common use case is finding all objects that match a query through the findInBackground()
method, using a FindCallback. For example, this sample code fetches all objects of class
"MyClass". It calls a different function depending on whether the fetch succeeded or not.
ParseQuery<ParseObject> query = ParseQuery.getQuery("MyClass");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
objectsWereRetrievedSuccessfully(objects);
} else {
objectRetrievalFailed();
}
}
}
A ParseQuery can also be used to retrieve a single object whose id is known, through the
getInBackground(String) method, using a GetCallback. For example, this
sample code fetches an object of class "MyClass" and id myId. It calls
a different function depending on whether the fetch succeeded or not.
ParseQuery<ParseObject> query = ParseQuery.getQuery("MyClass");
query.getInBackground(myId, new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
objectWasRetrievedSuccessfully(object);
} else {
objectRetrievalFailed();
}
}
}
A ParseQuery can also be used to count the number of objects that match the query without
retrieving all of those objects. For example, this sample code counts the number of objects of
the class "MyClass".
ParseQuery<ParseObject> query = ParseQuery.getQuery("MyClass");
query.countInBackground(new CountCallback() {
public void done(int count, ParseException e) {
if (e == null) {
objectsWereCounted(count);
} else {
objectCountFailed();
}
}
}
Using the callback methods is usually preferred because the network operation will not block the
calling thread. However, in some cases it may be easier to use the find(),
get(String) or count() calls, which do block the calling thread. For example,
if your application has already spawned a background task to perform work, that background task
could use the blocking calls and avoid the code complexity of callbacks.| Modifier and Type | Class and Description |
|---|---|
static class |
ParseQuery.CachePolicy
CachePolicy specifies different caching policies that could be used with
ParseQuery. |
static class |
ParseQuery.State<T extends ParseObject>
Used by Parse LiveQuery
|
| Modifier and Type | Field and Description |
|---|---|
static int |
MAX_LIMIT |
| Constructor and Description |
|---|
ParseQuery(java.lang.Class<T> subclass)
Constructs a query for a
ParseObject subclass type. |
ParseQuery(ParseQuery.State.Builder<T> builder) |
ParseQuery(ParseQuery<T> query)
Constructs a copy of
query; |
ParseQuery(java.lang.String theClassName)
Constructs a query.
|
| Modifier and Type | Method and Description |
|---|---|
ParseQuery<T> |
addAscendingOrder(java.lang.String key)
Also sorts the results in ascending order by the given key.
|
ParseQuery<T> |
addDescendingOrder(java.lang.String key)
Also sorts the results in descending order by the given key.
|
void |
cancel()
Cancels the current network request(s) (if any is running).
|
ParseQuery<T> |
clear(java.lang.String key)
Clears constraints related to the given key, if any was set previously.
|
static void |
clearAllCachedResults()
Clears the cached result for all queries.
|
void |
clearCachedResult()
Removes the previously cached result for this query, forcing the next find() to hit the
network.
|
int |
count()
Counts the number of objects that match this query.
|
<any> |
countInBackground()
Counts the number of objects that match this query in a background thread.
|
void |
countInBackground(CountCallback callback)
Counts the number of objects that match this query in a background thread.
|
java.util.List<T> |
find()
Retrieves a list of
ParseObjects that satisfy this query. |
<any> |
findInBackground()
Retrieves a list of
ParseObjects that satisfy this query from the source in a
background thread. |
void |
findInBackground(FindCallback<T> callback)
Retrieves a list of
ParseObjects that satisfy this query from the source in a
background thread. |
ParseQuery<T> |
fromLocalDatastore()
Change the source of this query to all pinned objects.
|
ParseQuery<T> |
fromNetwork()
Change the source of this query to the server.
|
ParseQuery<T> |
fromPin()
Change the source of this query to the default group of pinned objects.
|
ParseQuery<T> |
fromPin(java.lang.String name)
Change the source of this query to a specific group of pinned objects.
|
T |
get(java.lang.String objectId)
Constructs a
ParseObject whose id is already known by fetching data from the source. |
ParseQuery.State.Builder<T> |
getBuilder() |
ParseQuery.CachePolicy |
getCachePolicy() |
java.lang.String |
getClassName()
Accessor for the class name.
|
T |
getFirst()
Retrieves at most one
ParseObject that satisfies this query. |
<any> |
getFirstInBackground()
Retrieves at most one
ParseObject that satisfies this query from the source in a
background thread. |
void |
getFirstInBackground(GetCallback<T> callback)
Retrieves at most one
ParseObject that satisfies this query from the source in a
background thread. |
<any> |
getInBackground(java.lang.String objectId)
Constructs a
ParseObject whose id is already known by fetching data from the source in a
background thread. |
void |
getInBackground(java.lang.String objectId,
GetCallback<T> callback)
Constructs a
ParseObject whose id is already known by fetching data from the source in
a background thread. |
int |
getLimit()
Accessor for the limit.
|
long |
getMaxCacheAge()
Gets the maximum age of cached data that will be considered in this query.
|
static <T extends ParseObject> |
getQuery(java.lang.Class<T> subclass)
Creates a new query for the given
ParseObject subclass type. |
static <T extends ParseObject> |
getQuery(java.lang.String className)
Creates a new query for the given class name.
|
int |
getSkip()
Accessor for the skip value.
|
boolean |
hasCachedResult()
Returns whether or not this query has a cached result.
|
ParseQuery<T> |
ignoreACLs()
Ignore ACLs when querying from the Local Datastore.
|
ParseQuery<T> |
include(java.lang.String key)
Include nested
ParseObjects for the provided key. |
boolean |
isRunning() |
static <T extends ParseObject> |
or(java.util.List<ParseQuery<T>> queries)
Constructs a query that is the
or of the given queries. |
ParseQuery<T> |
orderByAscending(java.lang.String key)
Sorts the results in ascending order by the given key.
|
ParseQuery<T> |
orderByDescending(java.lang.String key)
Sorts the results in descending order by the given key.
|
ParseQuery<T> |
selectKeys(java.util.Collection<java.lang.String> keys)
Restrict the fields of returned
ParseObjects to only include the provided keys. |
ParseQuery<T> |
setCachePolicy(ParseQuery.CachePolicy newCachePolicy)
Change the caching policy of this query.
|
ParseQuery<T> |
setLimit(int newLimit)
Controls the maximum number of results that are returned.
|
ParseQuery<T> |
setMaxCacheAge(long maxAgeInMilliseconds)
Sets the maximum age of cached data that will be considered in this query.
|
ParseQuery<T> |
setSkip(int newSkip)
Controls the number of results to skip before returning any results.
|
ParseQuery<T> |
setTrace(boolean shouldTrace)
Turn on performance tracing of finds.
|
ParseQuery<T> |
whereContainedIn(java.lang.String key,
java.util.Collection<?> values)
Add a constraint to the query that requires a particular key's value to be contained in the
provided list of values.
|
ParseQuery<T> |
whereContains(java.lang.String key,
java.lang.String substring)
Add a constraint for finding string values that contain a provided string.
|
ParseQuery<T> |
whereContainsAll(java.lang.String key,
java.util.Collection<?> values)
Add a constraint to the query that requires a particular key's value to contain every one of
the provided list of values.
|
ParseQuery<T> |
whereContainsAllStartsWith(java.lang.String key,
java.util.Collection<java.lang.String> values)
Add a constraint to the query that requires a particular key's value to contain each one of
the provided list of strings entirely or just starting with given strings.
|
ParseQuery<T> |
whereDoesNotExist(java.lang.String key)
Add a constraint for finding objects that do not contain a given key.
|
ParseQuery<T> |
whereDoesNotMatchKeyInQuery(java.lang.String key,
java.lang.String keyInQuery,
ParseQuery<?> query)
Add a constraint to the query that requires a particular key's value does not match any value
for a key in the results of another
ParseQuery. |
ParseQuery<T> |
whereDoesNotMatchQuery(java.lang.String key,
ParseQuery<?> query)
Add a constraint to the query that requires a particular key's value does not match another
ParseQuery. |
ParseQuery<T> |
whereEndsWith(java.lang.String key,
java.lang.String suffix)
Add a constraint for finding string values that end with a provided string.
|
ParseQuery<T> |
whereEqualTo(java.lang.String key,
java.lang.Object value)
Add a constraint to the query that requires a particular key's value to be equal to the
provided value.
|
ParseQuery<T> |
whereExists(java.lang.String key)
Add a constraint for finding objects that contain the given key.
|
ParseQuery<T> |
whereFullText(java.lang.String key,
java.lang.String text)
Adds a constraint for finding string values that contain a provided
string using Full Text Search
|
ParseQuery<T> |
whereGreaterThan(java.lang.String key,
java.lang.Object value)
Add a constraint to the query that requires a particular key's value to be greater than the
provided value.
|
ParseQuery<T> |
whereGreaterThanOrEqualTo(java.lang.String key,
java.lang.Object value)
Add a constraint to the query that requires a particular key's value to be greater than or
equal to the provided value.
|
ParseQuery<T> |
whereLessThan(java.lang.String key,
java.lang.Object value)
Add a constraint to the query that requires a particular key's value to be less than the
provided value.
|
ParseQuery<T> |
whereLessThanOrEqualTo(java.lang.String key,
java.lang.Object value)
Add a constraint to the query that requires a particular key's value to be less than or equal
to the provided value.
|
ParseQuery<T> |
whereMatches(java.lang.String key,
java.lang.String regex)
Add a regular expression constraint for finding string values that match the provided regular
expression.
|
ParseQuery<T> |
whereMatches(java.lang.String key,
java.lang.String regex,
java.lang.String modifiers)
Add a regular expression constraint for finding string values that match the provided regular
expression.
|
ParseQuery<T> |
whereMatchesKeyInQuery(java.lang.String key,
java.lang.String keyInQuery,
ParseQuery<?> query)
Add a constraint to the query that requires a particular key's value matches a value for a key
in the results of another
ParseQuery. |
ParseQuery<T> |
whereMatchesQuery(java.lang.String key,
ParseQuery<?> query)
Add a constraint to the query that requires a particular key's value match another
ParseQuery. |
ParseQuery<T> |
whereNear(java.lang.String key,
ParseGeoPoint point)
Add a proximity based constraint for finding objects with key point values near the point
given.
|
ParseQuery<T> |
whereNotContainedIn(java.lang.String key,
java.util.Collection<?> values)
Add a constraint to the query that requires a particular key's value not be contained in the
provided list of values.
|
ParseQuery<T> |
whereNotEqualTo(java.lang.String key,
java.lang.Object value)
Add a constraint to the query that requires a particular key's value to be not equal to the
provided value.
|
ParseQuery<T> |
wherePolygonContains(java.lang.String key,
ParseGeoPoint point)
Add a constraint to the query that requires a particular key's
coordinates that contains a
ParseGeoPoints |
ParseQuery<T> |
whereStartsWith(java.lang.String key,
java.lang.String prefix)
Add a constraint for finding string values that start with a provided string.
|
ParseQuery<T> |
whereWithinGeoBox(java.lang.String key,
ParseGeoPoint southwest,
ParseGeoPoint northeast)
Add a constraint to the query that requires a particular key's coordinates be contained within
a given rectangular geographic bounding box.
|
ParseQuery<T> |
whereWithinKilometers(java.lang.String key,
ParseGeoPoint point,
double maxDistance)
Add a proximity based constraint for finding objects with key point values near the point given
and within the maximum distance given.
|
ParseQuery<T> |
whereWithinMiles(java.lang.String key,
ParseGeoPoint point,
double maxDistance)
Add a proximity based constraint for finding objects with key point values near the point given
and within the maximum distance given.
|
ParseQuery<T> |
whereWithinPolygon(java.lang.String key,
java.util.List<ParseGeoPoint> points)
Adds a constraint to the query that requires a particular key's
coordinates be contained within and on the bounds of a given polygon.
|
ParseQuery<T> |
whereWithinPolygon(java.lang.String key,
ParsePolygon polygon) |
ParseQuery<T> |
whereWithinRadians(java.lang.String key,
ParseGeoPoint point,
double maxDistance)
Add a proximity based constraint for finding objects with key point values near the point given
and within the maximum distance given.
|
public static final int MAX_LIMIT
public ParseQuery(java.lang.Class<T> subclass)
ParseObject subclass type. A default query with no further
parameters will retrieve all ParseObjects of the provided class.subclass - The ParseObject subclass type to retrieve.public ParseQuery(java.lang.String theClassName)
ParseObjects of the provided class.theClassName - The name of the class to retrieve ParseObjects for.public ParseQuery(ParseQuery<T> query)
query;query - The query to copy.public ParseQuery(ParseQuery.State.Builder<T> builder)
public static <T extends ParseObject> ParseQuery<T> or(java.util.List<ParseQuery<T>> queries)
or of the given queries.queries - The list of ParseQuerys to 'or' togetherParseQuery that is the 'or' of the passed in queriespublic static <T extends ParseObject> ParseQuery<T> getQuery(java.lang.Class<T> subclass)
ParseObject subclass type. A default query with no
further parameters will retrieve all ParseObjects of the provided class.subclass - The ParseObject subclass type to retrieve.ParseQuery.public static <T extends ParseObject> ParseQuery<T> getQuery(java.lang.String className)
ParseObjects of the provided class name.className - The name of the class to retrieve ParseObjects for.ParseQuery.public static void clearAllCachedResults()
public ParseQuery.State.Builder<T> getBuilder()
public void cancel()
public boolean isRunning()
public java.util.List<T> find() throws ParseException
ParseObjects that satisfy this query.
ParseObjects obeying the conditions set in this query.ParseException - Throws a ParseException if no object is found.ParseException.OBJECT_NOT_FOUNDpublic T getFirst() throws ParseException
ParseObject that satisfies this query.
Note:This mutates the ParseQuery.ParseObject obeying the conditions set in this query.ParseException - Throws a ParseException if no object is found.ParseException.OBJECT_NOT_FOUNDpublic ParseQuery.CachePolicy getCachePolicy()
public ParseQuery<T> setCachePolicy(ParseQuery.CachePolicy newCachePolicy)
fromLocalDatastore(),
fromPin(),
fromPin(String)public ParseQuery<T> fromNetwork()
setCachePolicy(CachePolicy)public ParseQuery<T> fromLocalDatastore()
setCachePolicy(CachePolicy)public ParseQuery<T> fromPin()
ParseObject.DEFAULT_PIN,
setCachePolicy(CachePolicy)public ParseQuery<T> fromPin(java.lang.String name)
name - the pinned groupsetCachePolicy(CachePolicy)public ParseQuery<T> ignoreACLs()
public long getMaxCacheAge()
public ParseQuery<T> setMaxCacheAge(long maxAgeInMilliseconds)
public <any> findInBackground()
ParseObjects that satisfy this query from the source in a
background thread.
This is preferable to using find(), unless your code is already running in a
background thread.Task that will be resolved when the find has completed.public void findInBackground(FindCallback<T> callback)
ParseObjects that satisfy this query from the source in a
background thread.
This is preferable to using find(), unless your code is already running in a
background thread.callback - callback.done(objectList, e) is called when the find completes.public <any> getFirstInBackground()
ParseObject that satisfies this query from the source in a
background thread.
This is preferable to using getFirst(), unless your code is already running in a
background thread.
Note:This mutates the ParseQuery.Task that will be resolved when the get has completed.public void getFirstInBackground(GetCallback<T> callback)
ParseObject that satisfies this query from the source in a
background thread.
This is preferable to using getFirst(), unless your code is already running in a
background thread.
Note:This mutates the ParseQuery.callback - callback.done(object, e) is called when the find completes.public int count()
throws ParseException
ParseException - Throws an exception when the network connection fails or when the query is invalid.public <any> countInBackground()
Task that will be resolved when the count has completed.public void countInBackground(CountCallback callback)
callback - callback.done(count, e) will be called when the count completes.public T get(java.lang.String objectId) throws ParseException
ParseObject whose id is already known by fetching data from the source.
Note:This mutates the ParseQuery.objectId - Object id of the ParseObject to fetch.ParseException - Throws an exception when there is no such object or when the network connection
fails.ParseException.OBJECT_NOT_FOUNDpublic boolean hasCachedResult()
public void clearCachedResult()
public <any> getInBackground(java.lang.String objectId)
ParseObject whose id is already known by fetching data from the source in a
background thread. This does not use caching.
This is preferable to using the ParseObject.createWithoutData(String, String), unless
your code is already running in a background thread.objectId - Object id of the ParseObject to fetch.Task that is resolved when the fetch completes.public void getInBackground(java.lang.String objectId,
GetCallback<T> callback)
ParseObject whose id is already known by fetching data from the source in
a background thread. This does not use caching.
This is preferable to using the ParseObject.createWithoutData(String, String), unless
your code is already running in a background thread.objectId - Object id of the ParseObject to fetch.callback - callback.done(object, e) will be called when the fetch completes.public ParseQuery<T> whereEqualTo(java.lang.String key, java.lang.Object value)
key - The key to check.value - The value that the ParseObject must contain.public ParseQuery<T> whereLessThan(java.lang.String key, java.lang.Object value)
key - The key to check.value - The value that provides an upper bound.public ParseQuery<T> whereNotEqualTo(java.lang.String key, java.lang.Object value)
key - The key to check.value - The value that must not be equalled.public ParseQuery<T> whereGreaterThan(java.lang.String key, java.lang.Object value)
key - The key to check.value - The value that provides an lower bound.public ParseQuery<T> whereLessThanOrEqualTo(java.lang.String key, java.lang.Object value)
key - The key to check.value - The value that provides an upper bound.public ParseQuery<T> whereGreaterThanOrEqualTo(java.lang.String key, java.lang.Object value)
key - The key to check.value - The value that provides an lower bound.public ParseQuery<T> whereContainedIn(java.lang.String key, java.util.Collection<?> values)
key - The key to check.values - The values that will match.public ParseQuery<T> whereContainsAll(java.lang.String key, java.util.Collection<?> values)
key - The key to check. This key's value must be an array.values - The values that will match.public ParseQuery<T> whereFullText(java.lang.String key, java.lang.String text)
Requires Parse-Server@2.5.0
key - The key to be constrained.text - String to be searchedpublic ParseQuery<T> whereContainsAllStartsWith(java.lang.String key, java.util.Collection<java.lang.String> values)
key - The key to check. This key's value must be an array.values - The values that will match entirely or starting with them.public ParseQuery<T> whereMatchesQuery(java.lang.String key, ParseQuery<?> query)
ParseQuery.
This only works on keys whose values are ParseObjects or lists of ParseObjects.key - The key to check.query - The query that the value should matchpublic ParseQuery<T> whereDoesNotMatchQuery(java.lang.String key, ParseQuery<?> query)
ParseQuery.
This only works on keys whose values are ParseObjects or lists of ParseObjects.key - The key to check.query - The query that the value should not matchpublic ParseQuery<T> whereMatchesKeyInQuery(java.lang.String key, java.lang.String keyInQuery, ParseQuery<?> query)
ParseQuery.key - The key whose value is being checkedkeyInQuery - The key in the objects from the sub query to look inquery - The sub query to runpublic ParseQuery<T> whereDoesNotMatchKeyInQuery(java.lang.String key, java.lang.String keyInQuery, ParseQuery<?> query)
ParseQuery.key - The key whose value is being checked and excludedkeyInQuery - The key in the objects from the sub query to look inquery - The sub query to runpublic ParseQuery<T> whereNotContainedIn(java.lang.String key, java.util.Collection<?> values)
key - The key to check.values - The values that will not match.public ParseQuery<T> whereNear(java.lang.String key, ParseGeoPoint point)
key - The key that the ParseGeoPoint is stored in.point - The reference ParseGeoPoint that is used.public ParseQuery<T> whereWithinMiles(java.lang.String key, ParseGeoPoint point, double maxDistance)
3958.8 miles.key - The key that the ParseGeoPoint is stored in.point - The reference ParseGeoPoint that is used.maxDistance - Maximum distance (in miles) of results to return.public ParseQuery<T> whereWithinKilometers(java.lang.String key, ParseGeoPoint point, double maxDistance)
6371.0 kilometers.key - The key that the ParseGeoPoint is stored in.point - The reference ParseGeoPoint that is used.maxDistance - Maximum distance (in kilometers) of results to return.public ParseQuery<T> whereWithinRadians(java.lang.String key, ParseGeoPoint point, double maxDistance)
key - The key that the ParseGeoPoint is stored in.point - The reference ParseGeoPoint that is used.maxDistance - Maximum distance (in radians) of results to return.public ParseQuery<T> whereWithinGeoBox(java.lang.String key, ParseGeoPoint southwest, ParseGeoPoint northeast)
key - The key to be constrained.southwest - The lower-left inclusive corner of the box.northeast - The upper-right inclusive corner of the box.public ParseQuery<T> whereWithinPolygon(java.lang.String key, java.util.List<ParseGeoPoint> points)
Polygon must have at least 3 points
key - The key to be constrained.points - Listpublic ParseQuery<T> whereWithinPolygon(java.lang.String key, ParsePolygon polygon)
public ParseQuery<T> wherePolygonContains(java.lang.String key, ParseGeoPoint point)
ParseGeoPoints
(Requires parse-server@2.6.0)
key - The key to be constrained.point - ParseGeoPointpublic ParseQuery<T> whereMatches(java.lang.String key, java.lang.String regex)
key - The key that the string to match is stored in.regex - The regular expression pattern to match.public ParseQuery<T> whereMatches(java.lang.String key, java.lang.String regex, java.lang.String modifiers)
key - The key that the string to match is stored in.regex - The regular expression pattern to match.modifiers - Any of the following supported PCRE modifiers:i - Case insensitive searchm - Search across multiple lines of inputpublic ParseQuery<T> whereContains(java.lang.String key, java.lang.String substring)
key - The key that the string to match is stored in.substring - The substring that the value must contain.public ParseQuery<T> whereStartsWith(java.lang.String key, java.lang.String prefix)
key - The key that the string to match is stored in.prefix - The substring that the value must start with.public ParseQuery<T> whereEndsWith(java.lang.String key, java.lang.String suffix)
key - The key that the string to match is stored in.suffix - The substring that the value must end with.public ParseQuery<T> include(java.lang.String key)
ParseObjects for the provided key.
You can use dot notation to specify which fields in the included object that are also fetched.key - The key that should be included.public ParseQuery<T> selectKeys(java.util.Collection<java.lang.String> keys)
ParseObjects to only include the provided keys.
If this is called multiple times, then all of the keys specified in each of the calls will be
included.
Note: This option will be ignored when querying from the local datastore. This
is done since all the keys will be in memory anyway and there will be no performance gain from
removing them.keys - The set of keys to include in the result.public ParseQuery<T> whereExists(java.lang.String key)
key - The key that should exist.public ParseQuery<T> whereDoesNotExist(java.lang.String key)
key - The key that should not existpublic ParseQuery<T> orderByAscending(java.lang.String key)
key - The key to order by.public ParseQuery<T> addAscendingOrder(java.lang.String key)
key - The key to order bypublic ParseQuery<T> orderByDescending(java.lang.String key)
key - The key to order by.public ParseQuery<T> addDescendingOrder(java.lang.String key)
key - The key to order bypublic int getLimit()
public ParseQuery<T> setLimit(int newLimit)
100,
with a maximum of 1000 results being returned at a time.newLimit - The new limit.public int getSkip()
public ParseQuery<T> setSkip(int newSkip)
newSkip - The new skippublic java.lang.String getClassName()
public ParseQuery<T> clear(java.lang.String key)
key - key to be cleared from current constraints.public ParseQuery<T> setTrace(boolean shouldTrace)