SwiftUI TabView with PageTabViewStyle calls onAppear of children multiple times on iOS 14.2

On iOS 14.2, when using the TabView in SwiftUI in combination with a PageTabViewStyle, the onAppear function of its children will be called excessively. When running the app on iOS 14, onAppear is called exactly once for each child when it appears.

Steps to reproduce:
  • Add the following code to a view:

Code Block
TabView {
Text("First text")
.onAppear {
print("First text appeared")
}
Text("Second text")
.onAppear {
print("Second text appeared")
}
Text("Third text")
.onAppear {
print("Third text appeared")
}
Text("Fourth text")
.onAppear {
print("Fourth text appeared")
}
}.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
  • run the app in a simulator or on a device

  • swipe through the tabs

Expected behavior:
  • The message is printed once when swiping between tabs.

Actual behavior:
  • (iOS 14.2) The message is printed multiple times when swiping between tabs.

  • (iOS 14) The message is printed once when swiping between tabs.

Radar: FB8907671
This situation has gotten even worse in iOS 14.3. The only reliable solution I've found is to abandon .onAppear/.onDisappear, use a @State var to track TabView transitions with .onChange, AND debounce calls to .onChange which can occur one or more times. See:

[https://stackoverflow.com/questions/64641759/swiftui-pagetabview-in-ios14-2-will-recall-childview-onappear-method-many-times/65542867#65542867)]
I have a "similar issue", I have a TabView with this configuration:
Code Block
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))


It's working fine, the swipe ok.. But with this line, the memory is never released! When I switch back and forth, my memory is climbing a lot. If I remove the swiping style, no problem at all..
I can't find any solution yet..
SwiftUI TabView with PageTabViewStyle calls onAppear of children multiple times on iOS 14.2
 
 
Q