iOS

Querying with NSPredicate

11min

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 -> iOS. Then select App. After you create your basic app you are ready to follow this guide.
Document image




Document image


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

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

2 - Open the project template

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


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

Document image


4. Wait for Xcode to open the project.

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 [query whereKey:@"playerName" notEqualTo:@"Michael Yabuti"]; 2 [query whereKey:@"playerAge" greaterThan:@18];

But a more complex query could become

1 [query whereKey:@"playerName" notEqualTo:@"Michael Yabuti"]; 2 [query whereKey:@"playerAge" greaterThan:@18]; 3 [query whereKey:@"playerHeight" greaterThan:@180]; 4 [query whereKey:@"playerWeight" greaterThan:@80]; 5 [query whereKey:@"playerFavoriteColour" notEqualTo:@"blue"]; 6 [query whereKey:@"playerIsLeftHanded" equalTo:@true]; 7 [query whereKey:@"playerShoeSize" notEqualTo:@42]; 8 [query whereKey:@"playerLivingState" equalTo:@"Arizona"]; 9 [query whereKey:@"playerLivingCity" notEqualTo:@"Springfield"]; 10 [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 NSPredicate *predicate = [NSPredicate predicateWithFormat: @"playerName != 'Michael Yabuti' AND playerAge > 18"]; 2 PFQuery *query = [PFQuery queryWithClassName:@"GameScore" predicate:predicate];

While a more complex query could become:

1 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'"]; 2 PFQuery *query = [PFQuery queryWithClassName:@"GameScore" predicate:predicate];

4 - Executing your Query

You can, then, execute your query:

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

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.