How to create or access a SwiftData Persistent Container in Swift

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

Code for a UIKit or AppKit application that enables you to access or create a SwiftData Persistent Container.

To access or create a persistent container in SwiftData, use the code below.

Please remember to replace _SwiftDataModelName_ with the name of the SwiftData model that you wish that container to access. For multiple models, use a comma-separated convention i.e. [ModelA.self, ModelB.self, ModelC.self].

We recommend that you place it within a Singleton called DataCoordinator that acts as a MainActor.

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.

For more information on the Persistent Container and what you can do with it, check out the WWDC videos linked 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