So I am working on a view in SwiftUI which will update its state when an event is published.
The view looks like this:
struct MyView: View {
@EnvironmentObject var dataSource: DataSource
@State var data: [Model] = []
func refreshData() {
self.data = dataSource.getData()
}
var body: some View {
VStack {
List(self.data) { model in
Row(model: model)
}
}
.onAppear {
self.refreshData()
}
.onReceive(self.dataSource.didUpdate) { _ in
print("on receive")
self.refreshData()
}
}
}
class DataSource: ObservableObject {
var didUpdate: PassthroughSubject<Model,Never> ...
}
So with this setup, the onAppear
block is called and works as expected. But the onReceive
callback is never called. I have been able to verify that .send
is being called correctly on the DataSource.didUpdate
subject, but it appears the subscriber is not being notified.
Is there something I am missing to make this work?