1

I have a List in SwiftUI App on MacOS with e.g. 10.000 entries.
Trying like the example below is horribly slow.

Adding .id(UUID()) to the List, which was advised in a prior post, makes it a bit quicker but still not fluid.
Even worst, adding .id(UUID()) to the list, the list then cannot be navigates by the arrow-key (up/down).

Is there a better way to achieve this?

struct TestViews_MacOS_BigList: View {
  @State var selectedItem: String?
  var items: [String]

  var body: some View {
    List(items,id: \.self, selection: $selectedItem ) { item in
      Text("\(item)").tag("\(item)")
    }
    //.id(UUID())
  }
}

func testnames()->[String]{
  var list: [String] = []
  for i in 1...10000 {
    list.append("Sting Nr \(i)")
  }
  return list
}
mica
  • 3,898
  • 4
  • 34
  • 62

1 Answers1

-2

Try using LazyVStack since it uses memory efficiently as below:

struct TestViews_MacOS_BigList: View {
 @State var selectedItem: String?
 var items: [String]
 var body: some View {
  ScrollView {
   LazyVStack{
    ForEach(items, id: \.self, content: { item in
     HStack{
      Button(action: {
       selectedItem = item
      }, label: {
       Text("Select ")
      })
      Text("\(item)").tag("\(item)")
    }
  })
 }
}

}

}

JohnD
  • 1