Interested in speaking at MongoDB World 2022? Click here to become a speaker.
HomeLearnHow-toGoodbye NSPredicate, hello Realm Swift Query API

Goodbye NSPredicate, hello Realm Swift Query API

Updated: Dec 13, 2021 |

Published: Dec 13, 2021

  • Mobile
  • Realm
  • Swift
  • ...

By Andrew Morgan

Rate this article


I'm not a fan of writing code using pseudo-English text strings. It's a major context switch when you've been writing "native" code. Compilers don't detect errors in the strings, whether syntax errors or mismatched types, leaving you to learn of your mistakes when your app crashes.

I spent more than seven years working at MySQL and Oracle, and still wasn't comfortable writing anything but the simplest of SQL queries. I left to join MongoDB because I knew that the object/document model was the way that developers should work with their data. I also knew that idiomatic queries for each programming language were the way to go.

That's why I was really excited when MongoDB acquired Realm—a leading mobile object database. You work with Realm objects in your native language (in this case, Swift) to manipulate your data.

However, there was one area that felt odd in Realm's Swift SDK. You had to use NSPredicate when searching for Realm objects that match your criteria. NSPredicates are strings with variable substitution. 🤦‍♂️

NSPredicates are used when searching for data in Apple's Core Data database, and so it was a reasonable design decision. It meant that iOS developers could reuse the skills they'd developed while working with Core Data.

But, I hate writing code as strings.

The good news is that the Realm SDK for Swift has added the option to use type-safe queries through the Realm Swift Query API. 🥳.

You now have the option whether to filter using NSPredicates:

1let predicate = NSPredicate(format: "isSoft == %@", NSNumber(value: wantSoft)
2let decisions = unfilteredDecisions.filter(predicate)

or with the new Realm Swift Query API:

1let decisions = unfilteredDecisions.where { $0.isSoft == wantSoft }

In this article, I'm going to show you some examples of how to use the Realm Swift Query API. I'll also show you an example where wrangling with NSPredicate strings has frustrated me.


#Using The Realm Swift Query API

I have a number of existing Realm iOS apps using NSPredicates. When I learnt of the new query API, the first thing I wanted to do was try to replace some of "legacy" queries. I'll start by describing that experience, and then show what other type-safe queries are possible.

#Replacing an NSPredicate

I'll start with the example I gave in the introduction (and how the NSPredicate version had previously frustrated me).

I have an app to train you on what decisions to make in Black Jack (based on the cards you've been dealt and the card that the dealer is showing). There are three different decision matrices based on the cards you've been dealt:

  • Whether you have the option to split your hand (you've been dealt two cards with the same value)
  • Your hand is "soft" (you've been dealt an ace, which can take the value of either one or eleven)
  • Any other hand

All of the decision-data for the app is held in Decisions objects:

1class Decisions: Object, ObjectKeyIdentifiable {
2 @Persisted var decisions = List<DecisionList>()
3 @Persisted var isSoft = false
4 @Persisted var isSplit = false
5 ...

SoftDecisionView needs to find the Decisions object where isSoft is set to true. That requires a simple NSPredicate:

1struct SoftDecisionView: View {
2 @ObservedResults(Decisions.self, filter: NSPredicate(format: "isSoft == YES")) var decisions
3 ...

But, what if I'd mistyped the attribute name? There's no Xcode auto-complete to help when writing code within a string, and this code builds with no errors or warnings:

1struct SoftDecisionView: View {
2 @ObservedResults(Decisions.self, filter: NSPredicate(format: "issoft == YES")) var decisions
3 ...

When I run the code, it works initially. But, when I'm dealt a soft hand, I get this runtime crash:

1Terminating app due to uncaught exception 'Invalid property name', reason: 'Property 'issoft' not found in object of type 'Decisions''

Rather than having a dedicated view for each of the three types of hand, I want to experiment with having a single view to handle all three.

SwiftUI doesn't allow me to use variables (or even named constants) as part of the filter criteria for @ObservedResults. This is because the struct hasn't been initialized until after the @ObservedResults is defined. To live within SwitfUIs constraints, the filtering is moved into the view's body:

1struct SoftDecisionView: View {
2 @ObservedResults(Decisions.self) var unfilteredDecisions
3 let isSoft = true
5 var body: some View {
6 let predicate = NSPredicate(format: "isSoft == %@", isSoft)
7 let decisions = unfilteredDecisions.filter(predicate)
8 ...

Again, this builds, but the app crashes as soon as I'm dealt a soft hand. This time, the error is much more cryptic:

1Thread 1: EXC_BAD_ACCESS (code=1, address=0x1)

It turns out that, you need to convert the boolean value to an NSNumber before substituting it into the NSPredicate string:

1struct SoftDecisionView: View {
2 @ObservedResults(Decisions.self) var unfilteredDecisions
5 let isSoft = true
8 var body: some View {
9 let predicate = NSPredicate(format: "isSoft == %@", NSNumber(value: isSoft))
10 let decisions = unfilteredDecisions.filter(predicate)
11 ...

Who knew? OK, StackOverflow did, but it took me quite a while to find the solution.

Hopefully, this gives you a feeling for why I don't like writing strings in place of code.

This is the same code using the new (type-safe) Realm Swift Query API:

1struct SoftDecisionView: View {
2 @ObservedResults(Decisions.self) var unfilteredDecisions
3 let isSoft = true
5 var body: some View {
6 let decisions = unfilteredDecisions.where { $0.isSoft == isSoft }
7 ...

The code's simpler, and (even better) Xcode won't let me use the wrong field name or type—giving me this error before I even try running the code:

Xcode showing the error "Binary operator '==' cannot be applied to operands of type 'Query<Boo>' and 'Int'
Xcode showing the error Binary operator '==' cannot be applied to operands of type 'Query<Bool>' and 'Int'

#Experimenting With Other Sample Queries

In my RCurrency app, I was able to replace this NSPredicate-based code:

1struct CurrencyRowContainerView: View {
2 @ObservedResults(Rate.self) var rates
3 let baseSymbol: String
4 let symbol: String
6 var rate: Rate? {
7 NSPredicate(format: "query.from = %@ AND = %@", baseSymbol, symbol)).first
8 }
9 ...

With this:

1struct CurrencyRowContainerView: View {
2 @ObservedResults(Rate.self) var rates
3 let baseSymbol: String
4 let symbol: String
6 var rate: Rate? {
7 rates.where { $0.query.from == baseSymbol && $ == symbol }.first
8 }
9 ...

Again, I find this more Swift-like, and bugs will get caught as I type/build rather than when the app crashes.

I'll use this simple Task Object to show a few more example queries:

1class Task: Object, ObjectKeyIdentifiable {
2 @Persisted var name = ""
3 @Persisted var isComplete = false
4 @Persisted var assignee: String?
5 @Persisted var priority = 0
6 @Persisted var progressMinutes = 0

All in-progress tasks assigned to name:

1let myStartedTasks = realm.objects(Task.self).where {
2 ($0.progressMinutes > 0) && ($0.assignee == name)

All tasks where the priority is higher than minPriority:

1let highPriorityTasks = realm.objects(Task.self).where {
2 $0.priority >= minPriority

All tasks that have a priority that's an integer between -1 and minPriority:

1let lowPriorityTasks = realm.objects(Task.self).where {
2 $0.priority.contains(-1...minPriority)

All tasks where the assignee name string includes namePart:

1let tasksForName = realm.objects(Task.self).where {
2 $0.assignee.contains(namePart)

#Filtering on Sub-Objects

You may need to filter your Realm objects on values within their sub-objects. Those sub-object may be EmbeddedObjects or part of a List.

I'll use the Project class to illustrate filtering on the attributes of sub-documents:

1class Project: Object, ObjectKeyIdentifiable {
2 @Persisted var name = ""
3 @Persisted var tasks: List<Task>

All projects that include a task that's in-progress, and is assigned to a given user:

1let myActiveProjects = realm.objects(Project.self).where {
2 ($0.tasks.progressMinutes >= 1) && ($0.tasks.assignee == name)

#Including the Query When Creating the Original Results (SwiftUI)

At the time of writing, this feature wasn't released, but it can be tested using this PR.

You can include the where modifier directly in your @ObservedResults call. That avoids the need to refine your results inside your view's body:

1@ObservedResults(Decisions.self, where: { $0.isSoft == true }) var decisions

Unfortunately, SwiftUI rules still mean that you can't use variables or named constants in your where block for @ObservedResults.


Realm type-safe queries provide a simple, idiomatic way to filter results in Swift. If you have a bug in your query, it should be caught by Xcode rather than at run-time.

You can find more information in the docs. If you want to see hundreds of examples, and how they map to equivalent NSPredicate queries, then take a look at the test cases.

For those that prefer working with NSPredicates, you can continue to do so. In fact the Realm Swift Query API runs on top of the NSPredicate functionality, so they're not going anywhere soon.

Please provide feedback and ask any questions in the Realm Community Forum.

Rate this article
MongoDB logo
© 2021 MongoDB, Inc.


  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.