Are changes to published embedded objects really not detected in SwiftUI?

Let's say I have a class to represent a user:

class User : Equatable, Codable, ObservableObject
{
	@Published var	ID: String = ""
	@Published var	username:  String = ""
	@Published var	firstName: String = ""
	@Published var	lastName: String = ""
	@Published var	EMail: String = ""
	@Published var	phoneNbr: String = ""
	@Published var	avatarURL: String = ""
	@Published var	mediaServiceID: String = ""
	@Published var	validated: Bool = false
...
}

I also have a controller ("viewmodel") to broker interactions between SwiftUI views and the User. It contains a User object as:

@MainActor class UserManager : ObservableObject
{
	@Published var user: User
...
}

And finally of course the view, into which I pass the UserManager upon initialization:

struct StartupView: View
{
	@ObservedObject var userMgr: UserManager
...
}

Changing published members of the User embedded in UserManager does not trigger a UI refresh. That strikes me as broken, since everything is published. Is it expected behavior?

This works as intended. Try if using the new Observation helps in your case.

The view that owns the ObservableObject must use the @StateObject property.

struct StartupView: View
{
	@StateObject var userMgr: UserManager
...
}

Use @ObservedObject in any other views that need access to the user manager.

How about trying this?

@MainActor class UserManager: ObservableObject {
    @Published var user: User
    
    private var cancelSet: Set<AnyCancellable> = .init()
    
    init() {
        self.user = User()
        self.user.objectWillChange.sink { [weak self] in
            self?.objectWillChange.send()
        }
        .store(in: &cancelSet)
        Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
            Task { @MainActor in
                self.user.ID += "1"
            }
        }
    }
}



struct ContentView: View {
    @ObservedObject var userManager: UserManager
    
    var body: some View {
        VStack {
            Text("\(userManager.user.ID)")
        }
        .padding()
    }
}

Are changes to published embedded objects really not detected in SwiftUI?
 
 
Q