简体   繁体   中英

SwiftUI ScrollView inside list has terrible performance implications

I have a really simple SwiftUI view that is listing a bunch of Texts:

extension String: Identifiable {
  public var id: String { return self }
}

struct ContentView: View {
  var items: [String] = (0..<1000).map { $0.description }
  var body: some View {
    List(items) { str in
      HStack {
        Text(str)
      }
    }
  }
}

This code seems to work fine and gives me smooth scroll performance.

If I change this so that the HStack is inside of a horizontally scrolling ScrollView:

var body: some View {
  List(items) { str in
    ScrollView(.horizontal) {
      HStack {
        Text(str)
      }
    }
  }
}

There is an enormous performance hit and memory appears to grow unbounded as I scroll up and down through the list. There aren't any leaks in the memory debugger.

I'm wondering if anyone knows why the performance hit is so massive and if there's any way around it.

Update:

The HStack and Text appear to be irrelevant to the issue, even a Spacer inside the scrollView will trigger the issue.

List(items) { _ in
  ScrollView(.horizontal) {
    Spacer()
  }
}

It seems like there might be a memory leak issue with at least List, ScrollView, Form, and NavigationView (see this question ). The author of that question filed feedback with Apple (FB7318839). I would encourage you to do so as well, and hopefully it's just a bug that gets fixed soon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM