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 1 hour
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.
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.
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.
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.