Querying with NSPredicate

back4app
3 min readJun 15, 2020

--

Introduction

In this article, 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;
  • Note: Follow the New Parse App tutorial to learn how to create a Parse app at Back4App;
  • Xcode;
  • Parse iOS SDK works with iOS 7.0 or higher.
  • Basic iOS app;

NOTE: If you don’t have a basic app created you can open Xcode and hit File-> New-> Project. Then select the single view app. After you create your basic app you are ready to follow this guide.

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.

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

4-Wait for Xcode to open the project.

Step 3 — Understanding 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:

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

But a more complex query could become

[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:

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

While a more complex query could become:

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:

[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 to Parse Open Source documentation for iOS SDK.

--

--

back4app
back4app

Written by back4app

Store and query relational data on the cloud. Make it accessible over GraphQL and REST with a scalable, open-source backend, based on the Parse Platform.

No responses yet