onAppear is called when the view hasn't appeared

I'm using a 100% SwiftUI app, currently on Xcode 12 Beta 4

In my ContentView, I have a simple TabView

Code Block swift
struct ContentView: View {
var body: some View {
TabView {
AgendaView()
.tabItem {
Image(systemName: "list.bullet")
Text("Agenda")
}
HealthView()
.tabItem {
Image(systemName: "heart")
Text("Health")
}
}
}
}


The AgendaView and HealthView have onAppear methods. On App launch, the AgendaView is the one visible, but onAppear is called for both AgendaView and HealthView.

Why is that? Shouldn't onAppear be called only when the view actually appears on screen?

Code for HealthView and AgendaView

Code Block swift
struct AgendaView: View {
var body: some View {
VStack {
Text("Hello, AgendaView!")
}.onAppear{
print("AgendaView.onAppear")
}.onDisappear(){
print("AgendaView.onDisappear")
}
}
}
struct HealthView: View {
var body: some View {
VStack {
Text("Hello, HealthView!")
}.onAppear{
print("HealthView.onAppear")
}.onDisappear(){
print("HealthView.onDisappear")
}
}
}




SOLUTION:

Used .task instead of .onAppear. .task runs whenever the view appears so it works the same without bugging out. .onDisappear still works fine so these can be used together to take the place of the paired usage of .onAppear and .onDisappear until there is a fix for .onAppear

(Tagging this for some poor soul who had my problem: Camera indicator turning on in view that doesn't use camera SwiftUI)

onAppear is called when the view hasn't appeared
 
 
Q