Memory management for image data storing and retrieving with SwiftData (or CoreData)

39 views Asked by At

I know how to work with Data type in SwiftData (or CoreData). But the one question I feel missed is what is the correct way to care about memory management in different cases.

For example, I have the model for image Data:

@Model class ImageModel {
    
    @Attribute(.externalStorage) let data: Data
    
    var uiImage: UIImage { .init(data: data) ?? .init() }
    
    init(data: Data) {
        self.data = data
    }
    
}

and the Item model:

@Model class Item {
    
    @Relationship(deleteRule: .cascade)
    var images: [ImageModel] = []

    init() {}
    
}

The problem started when I tried to show items in the list because it's so ineffectively to use computed var uiImage: UIImage { .init(data: data) ?? .init() } for every image in every item I scroll. I considered several options, such as making a small proxy image data display in ItemView or lazy loading.

How do you achieve continuous smooth image loading and displaying in the list?

I considered several options, such as making a small proxy image data display in ItemView or lazy loading.

0

There are 0 answers