How to get all SwiftData objects in Swift

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

A step by step guide on gathering all the existing SwiftData objects of a specific SwiftData Model using SwiftUI, UIKit or AppKit.

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 the SwiftData model that you wish to get all the objects for.

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 Functionality

A screenshot of Xcode showing the DataCoordinator+SwiftData.swift file. Highlighted is the functionality that allows us to fetch all the objects. Sample code found below.

Add functionality similar that below to the project to fetch all the objects for the model you created 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 about what you can do with the FetchDescriptor, we recommend checking out the links below.

Step Four: Fetch All Objects

A screenshot of XCode showing us calling the get all objects function. Highlighted on the screen is how the debug console prints an empty array. This is expected as we haven't added any objects yet.

Call the function defined in Step Three to get all the objects.

Run the app and confirm that the console shows the objects - which may be empty, if none have been added, as shown in the image above.

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.

Frequently Asked Questions (FAQ)

How do I sort the objects by creation date?

To sort the objects by creation date, you must add a date variable to the model (i.e. creationDate) and add the date of creation when creating a new object.

Then, when fetching the objects pass in a SortDescriptor that points to that variable using code similar to that below.

try persistantContainer.mainContext.fetch(FetchDescriptor<SampleSwiftDataModel>(sortBy: [SortDescriptor(.creationDate)]))

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