Use of SwiftData with iOS App

Use of SwiftData with iOS App

Write your model code declaratively to add managed persistence and automatic iCloud sync.

iOS 17.0+ Beta iPadOS 17.0+ Beta macOS 14.0+ Beta Mac Catalyst 17.0+ Beta tvOS 17.0+ Beta watchOS 10.0+ Beta

SwiftData was introduced in WWDC,23. It's an impressive way to write your model code with persistence storage, which could be really good exposure for apps which need a structured way of data storage with future references.

By the use of SwiftData, we could easily achieve a new direction of persistent storage by combining data model classes and modern concurrency features. SwiftData enables very minimal code changes without any external dependencies.

It makes code writing fast, efficient, and safe, enabling us to describe the entire model layer (or object graph) for our app.

SwiftData has uses beyond persisting locally created content. For example, an app that fetches data from a remote web service might use SwiftData to implement a lightweight caching mechanism and provide limited offline functionality like fetching the data at any moment, processing the data, display the results based on pre-stored data.

The use of SwiftData is more advanced than realM storage. It provides a structured layer of data storage, which could lead to analyze of the relationship between data and its mapping.

As our app’s model layer evolves, SwiftData automatically handles the migrations of the underlying model data so it remains in a consistent state. If the aggregate changes between two versions of the model layer exceed the capabilities of automatic migrations, use Schema and SchemaMigrationPlan to participate in those migrations and help them complete successfully.

Overview within Project

The sample project is designed to help understand the following

1. how to adopt SwiftData in an existing app which could have persistent storage with existing data model classes

2. How to store data model classes in SwiftData and add new records

3. How to update the record using the pre-existing SwiftData Storage

4. How to delete the record using the pre-existing SwiftData Storage

The EmployeesListWithSwiftData app fetches and displays all employees based on data recorded from the swift data storage, and allows to add, update, or remove employees for future reference.

By default, SwiftData behaves in the following way when determining where it persists data:

  1. It persists data stored in the app’s Application Support directory.

  2. This given app uses App Groups to access shared containers and share data between the SwiftData widget extension and the data storage host app. For an app that has the App Group entitlement (com.apple.security.application-groups), it persists the data stored in the root directory of the app group container. For apps that evolve from a version that doesn’t have any app group container to a version that has one, SwiftData copies the existing store to the app group container.

More Exposure could be taken by the setup of the GitHub project at Xcode.