sobabear
(Yongjun Lee)
1
Why concurrency codes are not working properly in command line?
like for exmaple
import Foundation
import Combine
class Concurrency {
let publisher = PassthroughSubject<Void, Never>()
let (stream, continuation) = AsyncStream<Void>.makeStream()
init() {
listen()
}
func sendEvent() {
Task { [weak self] in
self?.publisher.send()
self?.continuation.yield()
try! await Task.sleep(nanoseconds: 1_000_000_000)
self?.publisher.send()
self?.continuation.yield()
try! await Task.sleep(nanoseconds: 1_000_000_000)
self?.publisher.send()
self?.continuation.yield()
}
}
private func listen() {
// Different listening methods will demonstrate varying memory management behaviors
listen_4()
}
private func listen_4() {
Task { [weak self] in
if let values = self?.publisher.values {
for await _ in values {
print("publisher 4")
}
}
}
Task { [weak self] in
if let s = self?.stream {
for await _ in s {
print("stream 4")
}
}
}
}
}
var concurrency: Concurrency?
concurrency = .init()
concurrency?.sendEvent()
as you see it prints 6 times, but with 1second interval. It does work on playground or xcode, but not on others
including swift cli