Series: Leveraging SwiftData for Persistence

Leveraging SwiftData for Persistence

SwiftData is a new framework for persisting data purpose-build for Swift. It uses some underpinnings from CoreData, but usage in Swift could not be simpler. In this series we'll cover how to setup SwiftData, how to define models and relationships, and how to work with SwiftData to build an app that persists data using SwiftUI.

Length: about 2 hours


1. SwiftData - The Basics

Subscribers only

SwiftData is a replacement for CoreData, built entirely for Swift. It leverages the underpinnings of Core Data, but is much simpler to work with. In this video we will cover the first 3 main types you'll need to understand: ModelConfiguration, ModelContainer, and ModelContext.

2. The @Model Macro

Subscribers only

In this episode we will take a look at the new @Model macro, which we can use to decorate our model classes that we want to persist with SwiftData.

3. Inserting and Deleting records with the ModelContext

Subscribers only

This episode discusses how to work with the ModelContext in a SwiftUI app to insert and delete data. The ModelContext can be accessed through the ModelContainer, which is set up in the environment automatically. The @Query macro is used to retrieve artists from the database and iterate over them. We also implement swipe to delete using the onDelete modifier.

4. Building forms to create and edit models with SwiftData

Subscribers only

We learn how to build a form in SwiftData. We cover how to use the .sheet modifier to present a new view for creating or editing artist records. We also cover creating the state for the form, presenting the sheet, and creating the form itself. We also implement a scratch model context to ensure that our changes can be discarded if the user taps the Cancel button.

5. Editing Relationships with SwiftData

Subscribers only

In this episode we'll create a form to add and edit songs for a given artist. This will lean on the technique we used last time with a small workaround required.

6. Querying data with the @Query Macro

Subscribers only

In this episode we will provide a searchable interface using the @Query and #Predicate macros. We'll discuss how to use localizedStandardCompare instead of lowercase and other limitations of predicates. We'll also implement a solution for adding dynamic data to the query.

7. Migrating between versions of your model

Subscribers only

In this episode we cover migrating our Swift Data models between incompatible versions using custom and lightweight migration.