Relationships and Fetch Descriptors
Modelling one-to-many relationships and querying with FetchDescriptor.
SwiftData Relationships
Declare relationships between @Model classes using Swift properties. SwiftData infers the relationship type.
@Model
class Author {
var name: String
@Relationship(deleteRule: .cascade) var books: [Book] = []
init(name: String) { self.name = name }
}@Relationship Attribute
Use @Relationship to specify delete rules and inverse relationships explicitly.
@Model
class Book {
var title: String
var author: Author?
init(title: String) { self.title = title }
}All lessons in this course
- Core Data Stack: NSPersistentContainer Setup
- SwiftData @Model and ModelContext
- Relationships and Fetch Descriptors
- Lightweight Migrations and CloudKit Sync