如何使用Combine Publisher更改线程? [英] How to change thread using combine Publisher?

查看:49
本文介绍了如何使用Combine Publisher更改线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

推荐答案

组合-在撰写本文时-尚未完全集成在 Foundation 中.

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

根据Xcode 11 Beta 发行说明:

According to Xcode 11 Beta Release Notes:

Combine框架的Foundation集成不可用.以下与Combine相结合的Fo​​undation和Grand Central Dispatch集成不可用:KeyValueObserving,NotificationCenter,RunLoop,OperationQueue,Timer,URLSession,DispatchQueue,JSONEncoder,JSONDecoder,PropertyListEncoder,PropertyListDecoder和@Published属性包装器.(51241500)


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


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

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

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提供的

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

这篇关于如何使用Combine Publisher更改线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆