Chris Goringe
Chris Goringe

Reputation: 11

Does TabView miss navigation clicks while redrawing?

I have a vanilla TabView, an @ObservedObject, and a background task running which periodically updates the @Published field of the @ObservedObject.

I've noticed that when the background task runs more frequently, and generates more changes to the @ObservedObject, the navigation tabs on the TabView become less responsive - they 'miss' taps. If I tap two or three times, I can normally change the view. The more frequent the background updates, the more often taps are 'missed'.

So I'm wondering if the TabView somehow fails to notice taps while it is redrawing, or something like that?

Any insights?


import Foundation
import SwiftUI

struct ContentView2: View {
    enum Tabs {
        case main
        case audio
        case preferences
    }
    
    @State var tab = Tabs.preferences 
    @ObservedObject var appState = Globals.appState

    var body: some View {
        TabView(selection: $tab) {
            MainView()
                .tabItem {
                    Image(systemName: "video.fill")
                    Text("Main")
                }.tag(Tabs.main)
            AudioControlView()
                .tabItem {
                    Image(systemName: "speaker.fill")
                    Text("Audio")
                }.tag(Tabs.audio)
            PreferenceView()
                .tabItem {
                    Image(systemName: "gear")
                    Text("Prefs")
                }.tag(Tabs.preferences)
        }
    }
    
}

Upvotes: 0

Views: 48

Answers (1)

Chris Goringe
Chris Goringe

Reputation: 11

Found the problem...

The TabView doesn't need @ObservedObject var appState = Globals.appState because the TabView itself doesn't depend on the @Published fields.

Instead, the individual tabs (MainView() etc. ) need to observe.

So what was happening was that the TabView was being unnecessarily recomputed and redrawn for every change. Removing this line fixed everything.

Upvotes: 0

Related Questions