Home > Enterprise >  Why does SwiftUI not update a view here?
Why does SwiftUI not update a view here?

Time:09-10

The background color and text in ReadyDashboardView below don't update when isConnected is updated. Obviously I want to update the view when the connection is made. I was expecting that publishing everything in the chain to that variable would make it update instantly in swiftui. Instead, it's always rendering using the value provided when the variable is instantiated. Here's a very simplified look at the situation:

Is there another swiftui feature i should be using or am I going to have to make some sweeping changes to my codebase?

import SwiftUI

@main
struct TestApp: App {
    @StateObject private var env = PrinterEnv()
    
    var body: some Scene {
        WindowGroup {
            ReadyDashboardView()
                .environmentObject(env)
        }
    }
}

struct ReadyDashboardView: View {
    @EnvironmentObject var env: PrinterEnv

    var body: some View {
        VStack {
            HStack {
                Spacer()
                VStack {
                    Text(env.selectedPrinter?.isConnected ?? false ? "Printer Ready" : "Not Connected")
                        .padding(.bottom)
                }
                Spacer()
            }
            .background(env.selectedPrinter?.isConnected ?? false ? .green : .red)
           
        Button("Connect") { env.selectedPrinter?.isConnected = true }
        Button("Disconnect") { env.selectedPrinter?.isConnected = false }
            
        }
    }
}

class PrinterEnv: ObservableObject {
    @Published var configuredPrinters: [Printer] = []
    @Published var selectedPrinter: Printer?
    
    init() {
        configuredPrinters.append(contentsOf: [Printer()])
        selectedPrinter = configuredPrinters.first
    }
}

class Printer: ObservableObject {
    @Published var isConnected = false
}

CodePudding user response:

I suggest you do not nest ObservableObject, it does not work very well.

Try a Printer struct for example, such as:

struct Printer: Identifiable {
    let id = UUID()
    var isConnected = false
}

CodePudding user response:

I do not think the problem is specifically related to nested observable objects. Nesting them is working fine and is even recommended by community members as the best way to manage app wide states and ensure performance is acceptable.

See this: enter image description here

  • Related