【问题标题】:Race operator in Combine framework组合框架中的竞赛运算符
【发布时间】:2020-05-23 18:41:08
【问题描述】:

Combine 中是否有与race 运算符等效的运算符?它与CombineLatest 的不同之处在于它不会等待所有发布者至少发出一次。我现在使用的解决方法是将startWith 添加到每个发布者并过滤这些初始值

【问题讨论】:

标签: swift combine


【解决方案1】:

正如 cmets 所说,您可以使用 .merge(),然后简单地使用 .first() 获取第一个值:

cancellable = aPublisher
  .merge(anotherPublisher)
  .first()
  .sink { print($0) } 

//Will print the first value either from aPublisher or anotherPublisher,
//whichever arrives first.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 2010-12-02
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 2022-07-13
    • 2018-10-19
    相关资源
    最近更新 更多