Does SwiftUI List reuse its cells?

I have a set of SwiftData objects, which include a thumbnail and also HD image data. I also have SwiftUI List, presenting, some information, including thumbnail image.

The problem is, firstly, when I scroll, memory goes up and up, never seemingly releasing objects showed once.

Secondly, it seems, it loads in the memory the whole object, including HD data, not only thumbnail.

What can be the right way to work around here?

SwiftUI lists are lazy loaded: so they load when you scroll.

See discussion here: https://forums.developer.apple.com/forums/thread/651256

And yes, cells are reused.

Could you show the code ? If data contains thumbnail and HD, it is normal that both are loaded.

What did you expect ? What do you want ?

Could you create another State var, with only thumbnail, and show HD only when tapping the thumbnail ? It all depends on your spec.

Hi, Claude31, thank you for the answer.

So, the model looks like this:

class Item {
    var name: String = ""
    
    @Relationship(deleteRule: .cascade, inverse: \Picture.item)
    var pictures: [Picture] = []
    
    init(name: String = "", pictures: [Picture] = []) {
        self.name = name
        self.pictures = pictures
    }
}

@Model
class Picture {
    
    @Attribute(.externalStorage)
    var imageData: Data?
    var thumbNailData: Data?
    var item: Item?
    
    init(imageData: Data? = nil, thumbNailData: Data? = nil, item: Item? = nil) {
        self.imageData = imageData
        self.thumbNailData = thumbNailData
        self.item = item
    }
}

And the View looks like this:

struct ContentView: View {
    @Query private var items: [Item]
    
    var body: some View {
        NavigationStack {
            List {
                ForEach(items) { item in
                    HStack {
                        if !item.pictures.isEmpty, let pic = item.pictures.first?.thumbNailData, let uiImage = UIImage(data: pic) {
                            
                            Image(uiImage: uiImage)
                                .resizable()
                                .aspectRatio(contentMode: .fill)
                                .frame(width: 80, height: 80)
                                .clipShape(.rect(cornerRadius: 10))
                        }
                        
                        Text(item.name)
                    }
                }
            }
            .navigationTitle("Items: \(items.count)")
        }
    }
    
}

As it appears, it already gets over 300 MB of memory

and while it scrolls, it takes over 3 GB and never goes down.

So am I doing something in a wrong way?

Does SwiftUI List reuse its cells?
 
 
Q