I have a model that has a variable app_state defined briefly as so.
class AppState: ObservableObject {
@Published var status: NeStatus
The status is updated from a Task internal to AppState.
I'm using this status variable elsewhere (namely the status menu). I want to observe the changes to status in another asyc Task. The current way I'm thinking of doing is simply duplicating the Task code, however that's obviously not the ideal or most efficient manner.
In StatusItemManager's init
Task { @MainActor in
// get status using same way AppState does
// call code that will modify the status menu
}
My initial solution for the feature I'm working on was to poll the app_state.status every 5 seconds, which was deemed as bad code.
I'm a little new to combine as well so someone please correct me if this isn't quite right.
I think the whole idea of the @Published macro is that it creates a subject which will create an event whenever your status is set.
app_state.$status.sink { [weak self] newStatus in
// this closure should run every time your status is set
}
.store(&cancellables)
// you should keep a set to store this closure so its still in memory when your status changes