0

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)
        }
    }
    
}
  • `TabView` should totally work fine, but you may be doing some heavy calculations on the main thread inside your `ObservedObject`. Add code how you update `@Published` field – Phil Dukhov Sep 15 '21 at 07:50
  • Make sure the background updates are being done async – lorem ipsum Sep 15 '21 at 11:32

1 Answers1

0

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.