4

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?

0

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I tried, but nothing changed. But I tried only on top-level view inside the row. Do I need to set id on all views inside a top-level view?
this answer fixed it for me: hackingwithswift.com/articles/210/…
Awesome! Save my years!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.