Why does this view load twice?

Could someone please paste this ContentView.swift in a new SwiftUI project and run it a couple of times to see if you get the “TvAiringTodayView” message 2 times in the console when you click on the “TV” tab the first time? I have isolated it to this setup on my machine and see the following in my console when clicking the “TV” tab in the simulator:

initMainView

initMovieMainView

initTvMainView

initTvButtonBarView

initTvAiringTodayView

initTvAiringTodayView


It doesn't seem like the TvAiringTodayView should init twice when clicked, but it does.

import SwiftUI

struct ContentView: View {
    
    var body: some View {
        TabView {
            //Movies View
            MovieMainView()
                .tabItem {
                    Image(systemName: "film")
                    Text("Movies")
            }
            
            //TV View
            TvMainView()
                .tabItem {
                    Image(systemName: "tv")
                    Text("TV")
            }            
        }
    }
    
    init() {
        print("initMainView")
    }
}

struct MovieMainView: View {
    
    var body: some View {
        VStack {
            Text("MovieMainView")
        }
    }
    
    init() {
        print("initMovieMainView")
    }
}

struct TvMainView: View {
    
    var body: some View {
        VStack {
            ScrollView(.vertical, showsIndicators: false) {
                TvButtonBarView()
            }
        }
    }
    
    init() {
        print("initTvMainView")
    }
}

struct TvButtonBarView: View {
    @State private var selectedViewType = 0
    var viewType = ["On The Air", "Top Rated", "Popular"]
    
    var body: some View {
        VStack {
            Picker(selection: $selectedViewType, label: Text("Strength")) {
                ForEach(0 ..< viewType.count ) {
                    Text(self.viewType[$0])
                }
            }.pickerStyle(SegmentedPickerStyle())
            
            if selectedViewType == 0 {
                TvAiringTodayView()
            }
            //            if selectedViewType == 1 {
            //                TvTopRatedView()
            //            }
            //            if selectedViewType == 2 {
            //                TvPopularView()
            //            }
        }
    }
    
    init() {
        print("initTvButtonBarView")
    }
}

struct TvAiringTodayView: View {
    
    var body: some View {
        Text("TvAiringTodayView")
    }
    
    init() {
        print("initTvAiringTodayView")
    }
}

I tried it.

On XCode 11.3, iPhone 11 ProMax iOS 13.3 simulator

and

On XCode 11.4, iPhone SE (2nd generation iOS 13.4.1)



To get a clear picture, I added manually a line in log when app was loaded, before tapping on TV.


Here is the full log:

initMainView

initMovieMainView

initTvMainView

-- launch completed

initTvButtonBarView

initTvAiringTodayView


So, only one initTvAiringTodayView.


Why do you ask to run a couple of times ?

Thanks for the reply. When I try it for the first time on a new system the problem does not occur on the first click, but then it does occur on subsequent clicks, and then if you kill the app and rerun it then happens every time. It's such a strange problem to figure out.

Do you mean on device and / or simulator ?


I tried several times on simulator, did not succeed to reproduce.


Beyond the log, does it cause any problem in the app ?

If not, that probably means that (for unknow reason yet) a notification is sent twice).


Could you test with this change:

    @State private var selectedViewType = -1
I have the same issue during developing iOS app with Xcode 12 beta 2 for iOS 14 version...

I don't know what the reason is, and when I test above code in my Xcode 12 beta 2 but it didn't reproduce...

It must be a bug of SwiftUI , I think....

I didn't test my project in the real iPhone, but only in simulator yet.

I have the same issue in the current build of XCODE 12.5 and ios 14.6. Has somebody found an answer?

Why does this view load twice?
 
 
Q