How to store multiple images in swiftData?

I converted PhotosPickerItems into Data via loadTransferrable, and stored those into an array of data in @Model class, But whenever I try to retrieve it, processing time is very high. Has anybody come up wit an easy way to do this?

Replies

I'm interested to see if this is the same issue I'm experiencing. Does CPU usage remain high indefinitely? ie. ~100%? I'm also seeing memory utilisation spike but not continusouly growing.

I have a 1 to many relationship instead of directly an array of images.

My child class is like this:

@Model
final class SavedLocationImage {
    @Attribute(.externalStorage)
    var image: Data
   
    @Relationship(inverse: \SavedLocation.images)
        var location: SavedLocation
    
    init(image: Data) {
        self.image = image
    }
}

It seems to be that when I have one or more items in the collection it's causing continuous view updates.. I think

  • I've updated both XCode and the iOS Simulators today to Beta 8? and this problem seems to have been resolved in my app.

Add a Comment

I've noticed when I have an array of data like my imageArray below, if I explore the data stored locally by the simulator, the array doesn't seem to be stored in extern storage. I come to this conclusion because if I look at the Application Support directory & try to find the ".default_SUPPORT" hidden folder, it's not there. If I were to simply create a model of single images, each of the single images is saved in a ".default_SUPPORT" that I can see. So I'm not sure if setting up .externalStorage with an array of images is doing anything. I'd be curious if anyone has thoughts. Here is the struct I'm using that's working with my app, but that doesn't seem to actually be saving the array of images in .externalStorage, or so I've concluded due to the lack of the hidden ".default_SUPPORT" directory.:

import Foundation import SwiftData

@Model class Place { var city: String var country: String? @Attribute(.externalStorage) var imageArray: [Data?]

init(city: String, country: String? = nil, imageArray: [Data?] = []) {
    self.city = city
    self.country = country
    self.imageArray = imageArray
}

}