如何使用 combine Publisher 更改线程?

How to change thread using combine Publisher?

我正在使用 Combine 和 SwiftUI 做一些异步的事情,关键是我不知道如何从主线程中的异步操作接收响应。 apple doc 表示它可以用于 RunLoop.main,但目前在 Swift 5.0 中它不是调度程序。那么对此有什么想法吗?

我尝试按照 apple doc 使用,但没有成功。

anyPublisher
    .receiveOn(on: RunLoop.main)

Combine - 在撰写本文时 - 未完全集成到 Foundation

根据 Xcode 11 测试版 Release Notes:

The Foundation integration for the Combine framework is unavailable. The following Foundation and Grand Central Dispatch integrations with Combine are unavailable: KeyValueObserving, NotificationCenter, RunLoop, OperationQueue, Timer, URLSession, DispatchQueue, JSONEncoder, JSONDecoder, PropertyListEncoder, PropertyListDecoder, and the @Published property wrapper. (51241500)


根据最新的 Xcode 11 beta (2),此问题已得到修复,因此希望您的代码能够正常工作。

The Foundation integration for the Combine framework is now available. The following Foundation and Grand Central Dispatch integrations with Combine are available: KeyValueObserving, NotificationCenter, RunLoop, OperationQueue, Timer, URLSession, DispatchQueue, JSONEncoder, JSONDecoder, PropertyListEncoder, PropertyListDecoder, and the @Published property wrapper. (51241500)

感谢@Martin R 和@silicon_valley