Skip to content

New Tricks With Queries

We want data access via Parse to be fast and easy, no matter what sort of data you're storing. Recently we've launched a few new features to help you access your data in different ways.

Often you want to run multiple queries that differ in only the value of one parameter. For example, you might want to download song information for any one of a list of artists. Rather than issue separate queries for each artist, you can now grab all of that information with one query, using whereKey:containedIn:

// We want to get information for any of these artists
NSArray *artists = [NSArray arrayWithObjects:@"The Beatles",
                                             @"Them Crooked Vultures",

PFQuery *query = [PFQuery queryWithClassName:@"SongInfo"];
[query whereKey:@"artist" containedIn:artists];

[query findObjectsInBackgroundWithBlock:^(NSArray *songs, NSError *error) {
  // songs now contains SongInfo for any of the provided artists

Another pattern we've noticed is that sometimes you want to run a query only because you're interesting in counting the results. This introduces the overhead of sending the results when it isn't really necessary. To make this faster, we've introduced countObjects methods, similar to the findObjects methods.

In our SongInfo example, if you'd like to count how many songs there are by The Beatles:

// Construct a query
PFQuery *query = [PFQuery queryWithClassName:@"SongInfo"];
[query whereKey:@"artist" equalTo:@"The Beatles"];

// Use countObjects instead of findObjects
[query countObjectsInBackgroundWithBlock:^(int count, NSError *error) {
  // count tells you how many objects matched the query

Sometimes you want to page through data rather than access it all at once. For example, you might want to grab ten songs, and then another ten if the user swipes to see more. You can use query.skip and query.limit to control which results to retrieve. For example, to get the 51st through 100th most recent Beatles songs:

// Query for Beatles songs, most-recent-first
PFQuery *query = [PFQuery queryWithClassName:@"SongInfo"];
[query whereKey:@"artist" equalTo:@"The Beatles"];
[query orderByDescending:@"createdAt"];

// Skip the first 50, retrieve the next 50
query.skip = 50;
query.limit = 50;

[query findObjectsInBackgroundWithBlock:^(NSArray *songs, NSError *error) {
  // Now you have the 51st through 100th most recent Beatles songs

Check out the iOS query documentation for more on what you can do with the PFQuery. If you're working on Android, all of this functionality is available to you as well. Just check out the Android query documentation.

We're always trying to make Parse easier and to make more things possible using Parse. Your feedback is the fuel that keeps this engine running. So please, don't hesitate to send us feedback.

Thanks for reading!