TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: How can I fix slow list updates when using sections?

Forums > SwiftUI

The HWS article How to fix slow List updates in SwiftUI explains how to fix an issue with lists updating slowly. The example below is given, which demonstrates a large List loading slowly when it is shuffled (you may need to increase 1000 to a larger number to notice this).

struct ContentView: View {
    @State var items = Array(1...1000)

    var body: some View {
        VStack {
            Button("Shuffle") {
                self.items.shuffle()
            }

            List {
                ForEach(items, id: \.self) {
                    Text("Item \($0)")
                }                
            }
            // .id(UUID())
        }
    }
}

The fix, as explained in the article, is to add .id(UUID()) to the List as commented above, which completely eliminates the problem. My question is: is there a way to get this to work when the List includes sections? For example, the trick of adding .id(UUID()) no longer works if a section is added to the list above:

struct ContentView: View {
    @State var items = Array(1...1000)

    var body: some View {
        VStack {
            Button("Shuffle") {
                self.items.shuffle()
            }

            List {
                Section {
                    ForEach(items, id: \.self) {
                        Text("Item \($0)")
                    }
                }
            }
        }
    }
}

Adding .id(UUID()) no longer helps, whether it is added to the List, to the ForEach, to the Section or to any other combination of the three. Can anyone suggest a way around this issue? Thanks in advance!

2      

Good question for why it is not happening btw. Logically it should work as well, as it actually generates new UUID meaning the view should refresh without delay as well. Seems like processed in a different way than List without it. But from practical point of view, would you ever need sectioned list with thousands rows in it? ))) I doubt... the purpose of sections to visually separate rows on a screen. If you need to scroll such long list you won't be able to see what section you are currently in... so...

2      

Thanks for the reply @ygeras! My use case is displaying search results with a separate (small) section above showing the search parameters (this is the app I'm working on, the fourth screenshot showing the results screen is the view I'm having this issue with). I'd appreciate any suggestions for fixes or alternatives!

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

But what is your issue there? In Pauls example you are "regenerating" huge amount of rows while shuffling them. When you attached UUID to List it basically says: hey swiftui, the the whole list is different so refresh the list as the whole, no need to go inside and check which row is different and regenerate it. And with huge amount of rows it is a lot of work to do. With search you're filtering your data, which in turn provides you less rows that match your query. Do you experience delays while doing search or what is the issue?

2      

The 'search' being performed isn't related to this list or to this view. It occurs elsewhere in the app and, once complete, the results are passed to this view. I do not actually need to regenerate the list once it is created, but there are already significant delays when the list is created. Removing the sectionning and adding the id trick solves these, but I need a way to acheive this without losing the sections.

2      

I think I understood what was the problem. You don't need to touch the uppers sections in your view? If it only applied to bottom section. You can probably do like so:

Section {
   List {
     ForEach(items, id: \.self) {
       Text("Item \($0)")
     }
   }
   .id(UUID())
}

You need to put List inside Section and not Section inside a List

2      

Thanks for the suggestion! That indeed works, but then the problem is how to combine that with another section (otherwise the use of section is redundant). Are you suggesting something like this below?

Form {
   Text("Upper part")
   Section {
     List {
       ForEach(items, id: \.self) {
         Text("Item \($0)")
       }
     }
     .id(UUID())
   }
}

Unfortunately, when placing your code inside a Form like this the issue reappears.

2      

add UUID to Form

2      

This also works and if you add UUID to Vstack

struct ContentView: View {
    @State var items = Array(1...1000000)

    var body: some View {
        VStack {
            Button("Shuffle") {
                self.items.shuffle()
            }

            VStack {
                List {
                    Section {
                        ForEach(items, id: \.self) {
                            Text("Item \($0)")
                        }
                    }
                }
                .id(UUID())
            }
        }
    }
}

3      

Out-of-the-box idea: have you thought about:

  1. Create an enum (that conforms to CaseIterable) for the sections.
  2. Use a ForEach (on that enum) and identify the sections with an id of some kind. So you'd have a ForEach for sections (MySectionsEnum.allCases() ) and an inner one for the list.
  3. Include your list as previously mentioned?

3      

I suspect your real items are more interesting than the array of consecutive integers in your example. If so, how about making each item a struct with a value property and an id=UUID() property? Then you wouldn't have to worry about whether changing the layout breaks the fix (and you could omit the id in the ForEach).

3      

Thank you all for your suggestions! They're great ideas, but having tried them all sadly I don't think any solve the issue. I have however found a workaround to avoid running into this problem in the first place, so I'll mark this as solved. Thank you all!

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.