I want a "controller" or "viewmodel" object to be apprised of changes to an object in the data model. So I've set up a Combine publisher in the data-model object, which publishes whenever one of its member values changes:
import Foundationimport Combineclass User : Codable, Equatable, ObservableObject{ var ID: String = "" { didSet { changePublisher.send(self) } } var pw: String = "" { didSet { changePublisher.send(self) } } var username: String = "" { didSet { changePublisher.send(self) } } var firstName: String = "" { didSet { changePublisher.send(self) } } var lastName: String = "" { didSet { changePublisher.send(self) } } var validated: Bool = false { didSet { print("Publishing validated change.") changePublisher.send(self) } } let changePublisher = PassthroughSubject<User, Never>()...}
And here's the object that wants to know about changes to the above User. It gets the User passed into it as a member of the model:
class UserManager : ObservableObject{ var model: CentralModel var userChangePipeline: [AnyCancellable] = [] init(withModel: CentralModel) { model = withModel model.user.changePublisher.sink(receiveValue: { changedUser in print("Changed-user validation: \(changedUser.validated)"}) .store(in: &userChangePipeline) } ...}
The User object is printing the "publishing validated change" message when I alter that value, proving that the didSet
methods are called. But the subscriber's sink is never called. Note that I am saving the subscription.
Compounding this problem is that breakpoints are broken in Xcode 15.2.
I also tried changing the User to publish a string instead of itself, but the subscriber still doesn't receive any publication.