@KeithBauerANZ you convinced me yesterday on the need to switch to AsyncSequence, but I'm revisiting this a little bit and had some second thoughts.
While AsyncSequence sounds nice in theory, there are still many unsolved problems:
(1) AsyncStream
doesn't allow multiple observers, so it can't provide the same functionality as CurrentValueSubject
or PassthroughSubject
: 'Consuming an AsyncStream from multiple Tasks' Redux - #2 by FranzBusch or Swift Async Algorithms Proposal: Broadcast (Previously Shared) (many posts and many PRs not none got merged)
(2) lack of flatMapLatest/switchToLatest, it was proposed but not officially added: PrePitch: Introduce `(flat)mapLatest` Algorithms
(3) lack of the equivalent of .store(in: &cancellables)
there might still be limitations I haven't thought about yet.
There might be working solutions to those but then haven't reached consensus to get into even apple/swift-async-algorithms yet (let alone built-in framework), so the confidence isn't very high.
Now looking back at the Combine unsafe issue from this thread, you are right that unchecked sendable subject or publisher causes this loophole:
But we can say that directly passing a strong reference of self
into sink
isn't right anyways, i.e. retain cycle & memory leak. Let's say we have some linter to prevent this, so self
has to be captured before passing into sink
, then Swift 6 compiler actually warns us about the issue as expected:
which forces us to change it to:
then it should be safe.
What do you think?