Logo for tanaschita.com

How to store images in SwiftData

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

27 Nov 2023 · 1 min read

SwiftData allows us to store images by using the Data type, for example:

@Model
class Recipe {
var imageData: Data
}

When working with larger or many images, it might be a good idea to separate them from the model storage and store them in an external file. For that, SwiftData provides the .externalStorage attribute:

@Attribute(.externalStorage)
var imageData: Data?

With that in place, we tell SwiftData to store the image in a separate database which may lead to improved performance when working with larger amounts of data.

Sponsorship logo
Preparing for a technical iOS job interview - updated for iOS 18
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

persistence

swiftui

swift

ios

How to get a SwiftData model container and context in SwiftUI

Learn how to setup a SwiftData container and context directly in SwiftUI.

30 Oct 2023 · 4 min read

Latest articles and tips

© 2024 tanaschita.com

Privacy policy

Impressum