Skip to content

Hello, NSPredicate

Here at Parse, we believe that the frameworks you use to build your app should conform to your needs, not the other way around. That’s why we build native SDKs for all the platforms we support. And we integrate with unique features on each platform to make using our API feel right at home. For example, in JavaScript, we integrate with Backbone. On Windows, we support LINQ. Today, I’d like to tell you about a recent addition to this menagerie. On iOS and Mac OS X, we’ve added support for creating a PFQuery using NSPredicate and NSSortDescriptor. If you don’t know about NSPredicate and NSSortDescriptor, or just prefer the normal PFQuery methods, no need to worry. This isn’t replacing the old methods. In fact, to get the most out of PFQuery, we still recommend using the rest of its methods. However, for those of you who prefer NSPredicate, it’s a fine alternative. For example:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"playerName = 'Dan Stemkosk'"];
PFQuery *query = [PFQuery queryWithClassName:@"GameScore" predicate:predicate];
[query orderBySortDescriptor:[NSSortDescriptor sortDescriptorWithKey:@"score" ascending:NO]];

At this time, we support these NSPredicate features:

  • Simple comparisons such as =, !=, <, >, <=, >=, and BETWEEN with a key and a constant.
  • Containment predicates, such as x IN {1, 2, 3}.
  • Key-existence predicates, such as x IN SELF.
  • BEGINSWITH expressions.
  • Compound predicates with AND, OR, and NOT.
  • Sub-queries with "key IN %@", subquery.

See our docs for more details and examples.