如何使用Combine Publisher更改线程?

时间:2019-06-11 20:14:06

标签: swift swiftui combine

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

我尝试按照苹果文档使用,但是没有运气。

anyPublisher
    .receiveOn(on: RunLoop.main)

1 个答案:

答案 0 :(得分:3)

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

根据Xcode 11 Beta Release Notes

  

Combine框架的Foundation集成不可用。下列带有Combine的Foundation和Grand Central Dispatch集成不可用:KeyValueObserving,NotificationCenter,RunLoop,OperationQueue,Timer,URLSession,DispatchQueue,JSONEncoder,JSONDecoder,PropertyListEncoder,PropertyListDecoder和@Published属性包装器。 (51241500)   


根据最新的 Xcode 11 beta (2),此问题已修复,因此,您的代码可以正常工作。

  

现在可以使用Combine框架的Foundation集成。可以使用以下带有Combine的Foundation和Grand Central Dispatch集成:KeyValueObserving,NotificationCenter,RunLoop,OperationQueue,Timer,URLSession,DispatchQueue,JSONEncoder,JSONDecoder,PropertyListEncoder,PropertyListDecoder和@Published属性包装器。 (51241500)

感谢@Martin R和@silicon_valley提供update