I recently started looking into SwiftUI and have run through a few tutorials which recommend swapping views based on state (see the snippet below). However, I noticed while debugging that memory usage slowly creeps up with even the most basic UI. This may just be lack of knowledge but is it wrong to swap views in this sort of manner with SwiftUI?
Version 11.0 (11A420a) - iOS 13
// Memory Leak Test
struct ContentView: View {
@State private var toggle = false
func cycleViews() {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self.toggle = !self.toggle
self.cycleViews()
}
}
var body: some View {
Group {
if toggle {
ViewA()
} else {
ViewB()
}
}.onAppear {
self.cycleViews()
}
}
}
struct ViewA: View {
var body: some View {
VStack {
Text("Some Content")
Text("Some Content")
Text("Some Content")
Text("Some Content")
Text("Some Content")
}
}
}
struct ViewB: View {
var body: some View {
List {
Text("Some Content")
Text("Some Content")
Text("Some Content")
Text("Some Content")
Text("Some Content")
}
}
}