I've pretty much completed creating my first macOS SwiftUI app. It's working fine but after using it for more than 10 minutes I noticed a dramatic slow down and big memory footprint. I condensed the problem down into the following minimal example, which is a full SwiftUI app for both macOS and iOS.
import SwiftUI
@main
struct TestApp: App {
@State var selection: Int?
var body: some Scene {
WindowGroup {
NavigationView {
List(0...10, id: \.self) { element in
NavigationLink(destination: VeryComplexView(text: "\(element)"),
tag: element, selection: $selection) {
Text("\(element)")
}
}
}
}
}
}
struct VeryComplexView: View {
let text: String
var body: some View {
HStack {
ForEach(1...10, id: \.self) { _ in
List(1...5000, id: \.self) { element in
Text("\(text) \(element)")
}
}
}
}
}
I don't expect performance to be great, because it's really quit a big View, but I would expect performance to stay consistent and no memory leaks. But on macOS after just a few clicks in the NavigationView I'm getting a memory footprint of 1GB and performance decreases. Profiling the app with Leak Detector also shows a massive amount of leaks in SwiftUI classes. Compiling the same app for iOS stays at a reasonable footprint of 50 MB even after using it for a while. This bug prevents me from properly using my app. Are there workarounds?
I'm running the latest Big Sur 11.2.1 and Xcode 12.4