Is it ok for an Actor
type to have a Publisher
as a property to let others observe changes over time? Or use the @Published
property wrapper to achieve this?
actor MyActor {
var publisher = PassthroughSubject<Int, Never>()
var data: Int {
didSet {
publisher.send(data)
}
}
...
}
// Usage
var tasks = Set<AnyCancellable>()
let actor = MyActor()
Task {
let publisher = await actor.publisher
publisher.sink { print($0) }.store(in: &tasks)
}
This seems like this should be acceptable. I would expect a Publisher
to be thread safe, and as long as the Output
is a value type things should be fine.
I have been getting random EXC_BAD_ACCESS errors when using this approach. But turning on the address sanitizer causes these crashes to go away. I know that isn't very specific but I wanted to start by seeing if this type of pattern is ok to do.