Exploring the container
Until now, we discussed how to declare the different entities using the @Model
macro, define their relationships using the @Relationship
macro, and customize their attributes using the @
Attribute
macro.
However, we haven’t discussed setting up SwiftData to work with a schema and a persistent store.
When we delved into the @Model
macro, drawing parallels with Core Data was straightforward and remains so now. In Core Data, we set up the stack using NSPersistentContainer
, which encapsulates the different components, such as the data model, the store, and the context, into one stack that we can work with.
In SwiftData, we use ModelContainer
, which has the same responsibility.
Let’s try to understand how it works.
Setting up ModelContainer
ModelContainer
is essential for working with SwiftData. The reason is that SwiftData has three main components that the container encapsulates and wraps together:
- The data model: This is...