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)

 

Made with in Shangrao,China By 老雷

Copyright © devler.cn 1987 - Present

赣ICP备19009883号-1