SwiftData: Store Image Data, but Actually Outside the Database
Tunds (@tundsdev@iosdev.space) shared a video tutorial about SwiftData this weekend.
I hadn’t looked at SwiftData at all until now. The thumbnail read “Store Images in SwiftData”, and I was immediately worried: I recall it’s a bad idea to store megabyte upon megabyte of binary data inside the DB, assuming it’ll be using a BLOB
column.
Turns out that SwiftData isn’t that simplistic and my assumption is wrong if configured properly!
SwiftData models can manage storage outside to the database. That’s what @Attribute(.externalStorage)
is for: “Stores the property’s value as binary data adjacent to the model storage.”
Check out Tunds’s video, See How To Store An Image In SwiftData 📸. The last chapter is about this attribute.
Great to hear that SwiftData takes care of this so conveniently.