Day20 Combine 07 - Operators 其他常用操作符

breakpoint

breakpoint操作符可以发送调试信号来暂停进程的运行(只要在给定的闭包中返回 true),当上游发布任何消息时. 可用於制造 Debug 的中断点的方法

func breakpointDemo() {
        [1, 2].publisher
            .breakpoint(receiveSubscription: { subscription in
                return false // 返回 true 以抛出 SIGTRAP 中断讯号,调试器会被调起
            }, receiveOutput: { value in
                return false // 返回 true 以抛出 SIGTRAP 中断讯号,调试器会被调起
            }, receiveCompletion: { completion in
                return false // 返回 true 以抛出 SIGTRAP 中断讯号,调试器会被调起
            })
            .sink(receiveValue: { _ in
                
            })
            .store(in: &cancellables)
}

从上面的示例程序码中,我们可以看出,通过使用 breakpoint 操作符,我们可以很容易地在订阅操作、输出、完成发生时启用断点。
如果这时候想直接在程序码上打断点,我们就要重写 sink 部分的程序码,而且无法轻易地为订阅操作启用断点

handleEvents

handleEvents 操作符可以在发布事件发生时执行指定的闭包,介入所有上游的事件, 不会因为 RELEASE/DEBUG 的前置处理器差异

func handleEventsDemo() {
        [1, 2].publisher
            .handleEvents(receiveSubscription: { subscription in
                // 订阅事件
            }, receiveOutput: { value in
                // 值事件
            }, receiveCompletion: { completion in
                // 完成事件
            }, receiveCancel: {
                // 取消事件
            }, receiveRequest: { demand in
                // 请求需求的事件
            })
            .sink(receiveValue: { _ in
                
            })
            .store(in: &cancellables)
    }

zip

zip 操作符会将上游发布者发布的元素结合到一个流中,在每个上游发布者发送的元素配对时才向下游发送一个包含配对元素的元组。

func zipDemo() {
        let oddPublisher = PassthroughSubject<Int, Never>()
        let evenStringPublisher = PassthroughSubject<String, Never>()
        
        oddPublisher
            .zip(evenStringPublisher)
            .sink(receiveCompletion: { completion in
                print(completion)
            }, receiveValue: { value in
                print(value)
            })
            .store(in: &cancellables)
        
        oddPublisher.send(1)
        evenStringPublisher.send("2")
        oddPublisher.send(3)
        evenStringPublisher.send("4")
        evenStringPublisher.send("6")
        evenStringPublisher.send("8")
}
/*印出 
(1, "2")
(3, "4")
*/

merge

merge 操作符可以将上游发布者发送的元素合并到一个序列中。merge 操作符要求上游发布者的输出和失败类型完全相同。

func mergeDemo() {
      let oddPublisher = PassthroughSubject<Int, Never>()
      let evenPublisher = PassthroughSubject<Int, Never>()
      
      oddPublisher
          .merge(with: evenPublisher)
          .sink(receiveCompletion: { completion in
              print(completion)
          }, receiveValue: { value in
              print(value)
          })
          .store(in: &cancellables)
      
      oddPublisher.send(1)
      evenPublisher.send(2)
      oddPublisher.send(3)
      evenPublisher.send(4)
}
/*印出 
1
2
3
4
*/

<<:  Day20 Plugin 从零开始到上架 - 取得授权码(iOS)

>>:  资料结构和演算法

【Day 4】输出之後,BERT转换的Embedding怎麽用?

在此之前,我们已经介绍过BERT的核心概念迁移学习Transfer Learning以及它的输入输出...

Day-21 物件与原型链

JavaScript的物件基於「原型」的继承,可以令一个物件「继承另一个物件的属性」。具体上,以 O...

Day27:测试 Coroutine

Coroutine 是非同步程序的解决方案,我们将耗时的任务置放在 suspend 函式中,在正常的...

Day27 ( 游戏设计 ) 老鼠走迷宫

老鼠走迷宫 教学原文参考:老鼠走迷宫 这篇文章会介绍如何使用「创建角色」、「角色是否碰到其他角色」、...

Day22 类别与物件--魔术方法2 及 封装private

物件导向的封装特性 封装特性:在一个物件产生之後,物件的部分成员属性和成员方法在逻辑上是不允许在物件...