How to create or access a SwiftData Persistent Container in Swift
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.