How to get an object from SwiftData in Swift

Oscar de la Hera Gomez
Three flowers that represent Swift, Swift Data and Xcode.  The text "Get a SwiftData object" sits beneath them.

A step by step guide on gathering a specific object from a SwiftData persistent storage container using SwiftUI, UIKit or AppKit.

In order to get an object, you must have created a SwiftData Model and persistent container.

Once you have done this, you can create a function that uses a Predicate macro to search the model for an object using a filter.

To learn more about this consult the WWDC videos linked below.

A screenshot of an iPhone showing the result of the sample app available on our open source swift GitHub repository.

The following tutorial has been made available, along with a simple app for demonstrating SwiftData, through the tutorial/swift-data-basics branch on our Open Source Swift Starter Project.

git clone git@github.com:delasign/swift-starter-project.git

Step One: Create the Model

A screenshot of Xcode showing a completed model in the SampleSwiftDataModel.swift file.

In the project, create a SwiftData model for the objects.

We recommend that you use a unique attribute, to allow you to efficiently search for specifically objects.

Step Two: Create the Persistent Container

A screenshot of Xcode showing the DataCoordinator.swift file. Highlighted on the file is the code used for the persistent container within a main actor, and theres an empty array that will hold all the objects once we have fetched them. The sample code is below.

Create a persistent container and an array to hold the objects using code similar to that below.

Please remember to replace _SwiftDataModelName_ with the name of the model that you created in Step One.

We recommend placing this functionality within a Singleton that acts as a MainActor. If you do not use a MainActor, you will have to use Async/Await patterns.

Step Three: Add the Functionality

A screenshot of Xcode highlighting the functionality that is available in a code snippet below that allows you to get an object by id.

Add functionality similar that below to the project to fetch a single objects based on the model you created in Step One.

Please note that we are filtering by id, as this is the unique attribute that we declared in Step One.

We recommend adding this to a file called DataCoordinator+SwiftData.swift, where DataCoordinator is the name of the MainActor singleton that manages the SwiftData.

For more information on FetchDescriptors and Predicates, we recommend checking out the links below.

Looking to learn more about things you can do with SwiftData, Swift or XCode ?

Consult our quick start guide or search our blog to find educational content on learning how to use SwiftData, Swift or XCode.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details