79370495

Date: 2025-01-20 07:03:22
Score: 0.5
Natty:
Report link

Not sure if I should close this question (if someone thinks I should please vote so). There seems to be a much simpler and out-of-the-box solution for awaiting a value from a publisher. It's not a exact one-to-one replacement for my requirement, but this works for my use case for now.

I just need to call .values and that would convert the publisher to a AsyncPublisher. One could iterate the async stream and apply logic to wait till the the requirements dictate

@MainActor
class ThingClass {
    
    let intPTS = PassthroughSubject<Int, Never>()
    var anyCancellables: Set<AnyCancellable> = []
    
    init() {
        
        Timer.publish(every: 5, on: .main, in: .common)
            .autoconnect()
            .sink { _ in self.intPTS.send(10) }
            .store(in: &anyCancellables)
    }
    
    func doSomething() async {

        for await num in intPTS.values {
            if num == 10 {
                break
            }
        }
        print("intPTS is 10 now!")
    }
}
Reasons:
  • RegEx Blacklisted phrase (1): I should please
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
Posted by: Aswath