Logo for tanaschita.com

How to define one-to-many relationships in SwiftData

Learn to use SwiftData's @Relationship macro.

19 Feb 2024 · 4 min read

In this article, we will look at how to define one-to-many relationships in SwiftData. If you are new to SwiftData, check out this developer guide on SwiftData first.

Let's directly jump in.

Sponsorship logo
Preparing for a technical iOS job interview - updated for iOS 17
Check out my book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift, SwiftUI, Combine, HTTP Networking, Authentication, SwiftData & Core Data, Concurrency with async/await, Security, Automated Testing, Machine Learning and more.
LEARN MORE

Relationships in SwiftData can only be defined between reference types, i.e. classes. Let's look at an example of a one-to-many relationship:

@Model
class User {
@Relationship(inverse: \Note.user) var notes: [Note]?
init(notes: [Note]? = nil) {
self.notes = notes
}
}
@Model
class Note {
var user: User
var title: String
var text: String
init(user: User, title: String, text: String) {
self.user = user
self.title = title
self.text = text
}
}

In the example above, we use the @RelationShip macro to define a one-to-many relationship between a user and their notes. With that in place, if we now create a new Note for example, this note will automatically be added to the user's list of notes.

An interesting fact to know here is that when both relationship ends are declared as optional, SwiftData will automatically infer an inverse relationship even without the @RelationShip macro.

Furthermore, we can use the @Relationship macro to specify delete rules. By default, SwiftData uses the .nullify delete rule which only nullifies the related model's reference to the deleted model. That means that if in our example above a User gets deleted, then their notes will stay intact in our data store. In some cases that's the behaviour we want, but in our example, we'd like to delete the user's notes when the user gets deleted. For that, we can use the .cascade delete rule:

@Model
class User {
@Relationship(deleteRule: .cascade) var notes: [Note]?
}

With a .cascade rule, all user related notes will be deleted whenever the user is deleted.

Sponsorship logo
Preparing for a technical iOS job interview - updated for iOS 17
Check out my book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift, SwiftUI, Combine, HTTP Networking, Authentication, SwiftData & Core Data, Concurrency with async/await, Security, Automated Testing, Machine Learning and more.
LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

swiftdata

swiftui

swift

ios

How to store images in SwiftData

Learn how to efficiently store larger amounts of data in SwiftData.

27 Nov 2023 · 1 min read

Latest articles and tips

© 2024 tanaschita.com

Privacy policy

Impressum