iOS

Querying with NSPredicate

Introduction

In this section you will learn how to use NSPredicate to define your queries in Objective-C.

At any time, you can access the complete Project built with this tutorial at our GitHub repository.

Prerequisites

In this tutorial we will use a basic app created in Objective-C with Xcode 9.1 and iOS 11.

To complete this tutorial, you need:

  • An app created at Back4App.
  • Xcode.
  • Basic iOS app.
    • Note: If you don’t have a basic app created you can open Xcode and hit File-> New-> Project. Then select single view app. After you create your basic app you are ready to follow this guide.
      Creating intial Xcode project
      Setting the project to single view app

Note: Parse iOS SDK works with iOS 7.0 or higher.

Step 1 - Get the template

Download the template at
Back4App’s GitHub repository, and unzip files in your project folder.

You can do that using the following command line:

  $ curl -LOk https://github.com/back4app/ios-objective-c-quickstart-example/archive/master.zip && unzip master.zip

Step 2 - Open the project template

  1. Open Xcode.
  2. Click on File->Open.

    Xcode Welcome Screen

  3. Navigate to the project folder and double click on QuickStartObjcExampleApp.xcworkspace.

    Xcode Opening file

  4. Wait for Xcode to open the project.

Step 3 - Understandig the Difference

Usually for Objective-C you have two options for building queries: using the ‘PFQuery’ or the ‘NSPredicate’. Both work similarly but depending on how many constraints you want to use, it might make more sense using one instead of the other.

For instance, a simple query using PFQuery would be:

1
2
[query whereKey:@"playerName" notEqualTo:@"Michael Yabuti"];
[query whereKey:@"playerAge" greaterThan:@18];

But a more complex query could become

1
2
3
4
5
6
7
8
9
10
[query whereKey:@"playerName" notEqualTo:@"Michael Yabuti"];
[query whereKey:@"playerAge" greaterThan:@18];
[query whereKey:@"playerHeight" greaterThan:@180];
[query whereKey:@"playerWeight" greaterThan:@80];
[query whereKey:@"playerFavoriteColour" notEqualTo:@"blue"];
[query whereKey:@"playerIsLeftHanded" equalTo:@true];
[query whereKey:@"playerShoeSize" notEqualTo:@42];
[query whereKey:@"playerLivingState" equalTo:@"Arizona"];
[query whereKey:@"playerLivingCity" notEqualTo:@"Springfield"];
[query whereKey:@"playerMothersName" equalTo:@"Jane"];

So, depending on each case, you can choose to use ‘NSPredicate’ instead.
A simple query using ‘NSPredicate’ would be:

1
2
NSPredicate *predicate = [NSPredicate predicateWithFormat:   @"playerName != 'Michael Yabuti' AND playerAge > 18"];
PFQuery *query = [PFQuery queryWithClassName:@"GameScore" predicate:predicate];

While a more complex query could become:

1
2
NSPredicate *predicate = [NSPredicate predicateWithFormat:   @"playerName != 'Michael Yabuti' AND playerAge > 18 AND playerHeight > 180 AND playerWeight > 80 AND playerFavoriteColour != 'blue' AND playerIsLeftHanded = true AND playerShoeSize != 42 AND playerLivingState = 'Arizona' AND playerLivingCity != 'Springfield' AND playerMothersName = 'Jane'"];
PFQuery *query = [PFQuery queryWithClassName:@"GameScore" predicate:predicate];

Step 4 - Executing your Query

You can, then, execute your query:

1
2
3
4
5
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
  if (!error) {
    // Request succeeded
  }
}];

Next Steps

At this point, you have learned how to get started with iOS apps.

Learn more by walking around our iOS Tutorials or check Parse open source documentation for iOS SDK.