【问题标题】:Is it possible a pipeline of loop been created using Publisher and Subscriber in Swift Combine framework?是否可以在 Swift Combine 框架中使用发布者和订阅者创建循环管道?
【发布时间】:2021-05-03 09:21:24
【问题描述】:

以下代码不适用于 EXC_BAD_ACCESS 错误。

func publisherLoopExample() {
    let publisher = PassthroughSubject<String, Never>()
    
    let subscriber = publisher.sink { [weak publisher] received in
        guard let publisher = publisher else { return }
        publisher.send(received)
        print("received \(received)")
    }
    
    publisher.send("1")
}

问题:

  1. 为什么会引发 EXC_BAD_ACCESS?
  2. 是否担心订阅者会将数据发送给原始发布者,从而导致循环?

【问题讨论】:

  • 这里的一切都是同步发生的。 send 调用 sink,后者调用 send,后者又调用 sink,依此类推……您创建了一个紧密循环。
  • 您遇到了这个站点被命名的问题:堆栈溢出。

标签: swift combine


【解决方案1】:

第一季度: 查看调用堆栈,它确实重复了很多次接收器,这可能会导致溢出。

第二季度: 综上所述,是的,应该避免。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多