feedback-assistant / reports

Open collection of Apple Feedback Assistant reports

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FB13103224: SwiftUI.TabView's tab useless onAppear call occurs when TabView is removed

yimajo opened this issue Β· comments

  • Date: 2023-09-02
  • Resolution: Open
  • Area: SwiftUI
  • OS: iOS 17 beta 8
  • Type: incorrect/Unexpected Behavior
  • Keywords:

Details

Description

When SwiftUI.TabView is removed from the View, each Tab unnecessarily calls .onAppear.

Condition. There must be multiple tabs, and each of the multiple tabs must be visible at least once. Specifically, this occurs when the Home screen is displayed in the TabView, and the tab is destroyed by logging out after switching to the profile screen.

import SwiftUI

struct ContentView: View {
    @State var loggedIn: Bool = false

    var body: some View {
        switch loggedIn {
        case false:
            Button("Login") {
                loggedIn = true
            }
        case true:
            TabView {
                NavigationView {
                    Text("Home")
                        .navigationBarTitle("Home")
                }
                .tabItem {
                    Image(systemName: "house")
                    Text("Home")
                }
                .onAppear {
                    print("🍏 Home on appear πŸ’’πŸ’’πŸ’’πŸ’’")
                }
                .onDisappear {
                    print("🍎 Home on disappear")
                }

                NavigationView {
                    Text("Profile")
                        .navigationBarTitle("Profile")
                        .toolbar {
                            ToolbarItem(placement: .navigationBarTrailing) {
                                Button("Logout") {
                                    loggedIn = false
                                }
                            }
                        }
                }
                .tabItem {
                    Image(systemName: "person")
                    Text("Profile")
                }
                .onAppear {
                    print("🍏 Profile on appear")
                }
                .onDisappear {
                    print("🍎 Profile on disappear")
                }
            }
        }
    }
}

Actual logs...

🍏 Home on appear πŸ’’πŸ’’πŸ’’πŸ’’
🍏 Profile on appear
🍎 Home on disappear
🍏 Home on appear πŸ’’πŸ’’πŸ’’πŸ’’
🍎 Home on disappear
🍎 Profile on disappear

Expected Logs...

🍏 Home on appear πŸ’’πŸ’’πŸ’’πŸ’’
🍏 Profile on appear
🍎 Home on disappear
🍎 Profile on disappear

This is true not only for onAppear but also for task.

Please refer to the Developer Forum for more information.
https://developer.apple.com/forums/thread/719521?login=true

Files