How to change thread using combine Publisher?

∥☆過路亽.° 提交于 2019-12-02 02:00:52

问题


I am using Combine and SwiftUI to do some async stuff, the point is that I don't know how to receive the response from the asynchronous operation in the main thread. The apple doc says that it can be used the RunLoop.main, but currently in Swift 5.0 it isn't a Scheduler. So any ideas about this?

I have tried to use as per apple doc, but no luck.

anyPublisher
    .receiveOn(on: RunLoop.main)

回答1:


Combine - at the time of writing - is not fully integrated in Foundation.

According to Xcode 11 Beta 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)


As per the latest Xcode 11 beta (2), this has been fixed, so expect your code to work correctly.

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)

Thanks to @Martin R and @silicon_valley for the update.



来源:https://stackoverflow.com/questions/56551108/how-to-change-thread-using-combine-publisher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!