Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
  }
}
like image 625
Thomas Abend Avatar asked Sep 25 '19 13:09

Thomas Abend


1 Answers

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.

like image 195
John M. Avatar answered Nov 10 '22 16:11

John M.