Post

Replies

Boosts

Views

Activity

Reply to AsyncPublisher of KeyValueObservingPublisher doesn't work
This appears to be a bug in NSObject.KeyValueObservingPublisher. It's triggered because the values property only sends an initial Demand of .max(1) when it subscribes to the KVO publisher. Forcing a larger Demand works around the problem. We can use the handleEvents operator to send unlimited demand to the KVO publisher: Task { for await value in obj .publisher(for: \.count) .handleEvents(receiveSubscription: { $0.request(.unlimited) }) .values { print("async: \(value)") } }
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’24
Reply to Associated Code?
actor ImageDownloader { private enum CacheEntry { case inProgress(Task.Handle<Image, Error>) case ready(Image) } private var cache: [URL: CacheEntry] = [:] func image(from url: URL) async throws -> Image? { if let cached = cache[url] { switch cached { case .ready(let image): return image case .inProgress(let handle): return try await handle.get() } } let handle = async { try await downloadImage(from: url) } cache[url] = .inProgress(handle) do { let image = try await handle.get() cache[url] = .ready(image) return image } catch { cache[url] = nil throw error } } }
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’21