Operator之处理线程(Scheduler and Thread handling operators)
subscribe/receive一般搭配使用,常用场景:数据流在后台处理,处理完的数据以后,切换至主线程刷新UI
networkDataPublisher
.subscribe(on: backgroundQueue)
.receive(on: RunLoop.main)
.assign(to: \.text, on: yourLabel)
示例
let publisher = PassthroughSubject<Int, Error>()
let myBgQueue = DispatchQueue(label: "myBgQueue")
cancellable = publisher
.subscribe(on: myBgQueue)
.receive(on: RunLoop.main)
.sink(receiveCompletion: {
print($0)
/// 刷新UI
}, receiveValue: {
print($0)
})
publisher.send(1)