I was seeing some behavior I couldn't understand with onAppear and my tabs (Xcode 12 beta 3). I wrote a quick project with two tabs to test. Here's tab1 (tab2 is the same).
var body: some View {
NavigationView {
Text("Tab1")
}
.onAppear() {
let datefor = DateFormatter()
datefor.timeStyle = .medium
print("onAppear tab1 \(datefor.string(from: Date()))")
}
.onDisappear() {
print("onDisappear tab1")
}
}
At app launch:
onAppear tab1 10:29:18 AM
onAppear tab2 10:29:18 AM
Touch tab2:
onDisappear tab1
onDisappear tab2
onAppear tab2 10:29:42 AM
onAppear tab1 10:29:42 AM
Now this is where it gets interesting. Touch tab1 and the wrong onAppear gets called:
onDisappear tab2
onAppear tab2 10:30:10 AM
Touch tab2 and the same thing happens:
onDisappear tab1
onAppear tab1 10:30:46 AM
var body: some View {
NavigationView {
Text("Tab1")
}
.onAppear() {
let datefor = DateFormatter()
datefor.timeStyle = .medium
print("onAppear tab1 \(datefor.string(from: Date()))")
}
.onDisappear() {
print("onDisappear tab1")
}
}
At app launch:
onAppear tab1 10:29:18 AM
onAppear tab2 10:29:18 AM
Touch tab2:
onDisappear tab1
onDisappear tab2
onAppear tab2 10:29:42 AM
onAppear tab1 10:29:42 AM
Now this is where it gets interesting. Touch tab1 and the wrong onAppear gets called:
onDisappear tab2
onAppear tab2 10:30:10 AM
Touch tab2 and the same thing happens:
onDisappear tab1
onAppear tab1 10:30:46 AM