Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI List freezes on @ObservedObject update

I have the List which automatically fetches more data near the end:

struct AdCardListView: View {
    @ObservedObject var model: AdListViewModel = AdListViewModel()

    var body: some View {
        List { ForEach(self.model.adArray.enumerated().map({ $0 }), id: \.element.id) { index, ad in
                AdCardView(ad: ad)
                    .onAppear {
                        DispatchQueue.main.async {
                            let count = self.model.adArray.count
                            if index >= count - 5 { //5 Views to the end, start loading more.
                                self.model.fetch(count: count)
                            }
                        }
                }
                }
            }
    }
}

Model looking like:

final class AdListViewModel: ObservableObject {
    @Published var adArray = [Ad]()
    ...
 func fetch(count: Int = 0) {
    ...
    DispatchQueue.main.async {
        self.adArray.append(contentsOf: newAds) //<-- problem here
    }
    ...
}

My problem: every @Published/@ObservedObject modification I have a little freeze when scroll list. Also, I found that List recalculates the body of all visible views + a few views above and below.

But I can't determine what leads to hangs of scrolling and fix it (maybe freeze is a transfer to a distance equal to (scrolling speed * rendering time)?

Why SwiftUI recalculate bodies on the existing views? They have not changed!

Can you help me?

like image 306
Fedyanint Timofey Avatar asked Oct 27 '25 05:10

Fedyanint Timofey


1 Answers

Recreation of the body should not be an issue.I doubt that the performance issue is related with the update animations of the List.

Did you try adding .id(UUID()) modifier to your List so that animations are discarded and the list is recreated after updates.

like image 158
Ilker Baltaci Avatar answered Oct 29 '25 07:10

Ilker Baltaci



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!