Operator之顺序操作(Applying sequence operations to elements)

first

first当收到第一个数据后,就会结束该pipline

_ = [1, 2, 3, 4]
    .publisher
    .first()
    .sink(receiveCompletion: { completion in
        print("结束了")
        switch completion {
        case .finished:
            print("完成")
        case .failure(let error):
            print("错误:\(error.localizedDescription)")
        }
    }, receiveValue: { someValue in
        print("someValue: \(someValue)")
    })

firstWhere

获取到第一个符合条件的数据后即接收pipline

.first { (incomingobject) -> Bool in
    return incomingobject.count > 3 
}

tryFirst

.tryFirst { (incomingobject) -> Bool in
    if (incomingobject == "boom") {
        throw TestExampleError.invalidValue
    }
    return incomingobject.count > 3
}

last/lastWhere/tryLastWhere

lastfirst刚好对应,取最后一个符合的元素

.last { (incomingobject) -> Bool in
    return incomingobject.count > 3 
}

.tryLast { (incomingobject) -> Bool in
    if (incomingobject == "boom") { 
        throw TestExampleError.invalidValue
    }
    return incomingobject.count > 3 
}

drop

drop有3种用法:

  • dropUntilOutput

  • dropWhile

  • tryDropWhile

dropUntilOutput,允许我们用另一个publisher来触发当前的publisher

/// 发送数据
let publisher = PassthroughSubject<String, Never>()
/// 触发
let triggerPublisher = PassthroughSubject<Int, Never>()

_ = publisher
    .drop(untilOutputFrom: triggerPublisher)
    .sink(receiveCompletion: { completion in
        print("结束了")
        switch completion {
        case .finished:
            print("完成")
        case .failure(let error):
            print("错误:\(error.localizedDescription)")
        }
    }, receiveValue: { someValue in
        print("someValue: \(someValue)")
    })

publisher.send("你好")
triggerPublisher.send(1)
publisher.send("张三")
publisher.send(completion: Subscribers.Completion.finished)

dropWhile/tryDropWhile

dropWhile过滤掉的是一开始闭包返回true的数据,当闭包第一次返回false,就不会再继续过滤数据,不管数据是否符合闭包的条件,都会流向下一个阶段。

_ = [-40, -10, 0, 10, 0, 2, -30]
    .publisher
    .drop { $0 <= 0 }
    .sink { print($0) }

[-40, -10, 0, 10, 0, 2, -30]在过滤掉数据 -40, -10, 0 后继续进行接下去的处理

prepend

Publisher 按先后顺序处理数据流,只有当前者发送.finished事件后,后者才继续接收数据

let firstPublisher = PassthroughSubject<String, Never>()
let secondPublisher = PassthroughSubject<String, Never>()

secondPublisher
.prepend(firstPublisher)

也可以直接使用

Publishers.Concatenate(prefix: firstPublisher, suffix: secondPublisher)

示例如下

let secondPublisher = PassthroughSubject<String, Never>()

secondPublisher
    .prepend(["1", "2", "3"])
    .sink(receiveCompletion: { completion in
        ...
    }, receiveValue: { someValue in
        print("someValue: \(someValue)")
    })
    .store(in: &cancellables)

secondPublisher.send("4")

打印结果如下:

someValue: 1
someValue: 2
someValue: 3
someValue: 4

dropFirst

.dropFirst的过滤掉pipline中的第一个数据,.dropFirst(n)指定了某个数值后,就会过滤掉pipline最开始n个数的数据

 

prefixUntilOutput

firstPublisher.prefix(untilOutputFrom:secondPublisher)则表示当second publisher发送第一个数据后,frist publisher就会立刻终止该pipline。

let firstPublisher = PassthroughSubject<Int, Never>()
let secondPublisher = PassthroughSubject<Int, Never>()

firstPublisher
    .prefix(untilOutputFrom: secondPublisher)
    .sink(receiveCompletion: { completion in
        print("结束了")
        switch completion {
        case .finished:
            print("完成")
        case .failure(let error):
            print("错误:\(error.localizedDescription)")
        }
    }, receiveValue: { someValue in
        print("someValue: \(someValue)")
    })
    .store(in: &cancellables)

firstPublisher.send(1)
firstPublisher.send(2)

secondPublisher.send(3)
firstPublisher.send(4)

打印结果:

someValue: 1
someValue: 2
结束了
完成

.prefix(n)会在收到n个数据后结束pipline

 

prefixWhile/tryPrefixWhile

prefix(while:)也是publisher的限制条件,while是一个闭包参数,它接收上游输出的数据,返回Bool值,当该闭包第一次返回false的时候,该pipline就会立马结束

[1, 2, 3, 4]
    .publisher
    .prefix { value -> Bool in
        value <= 2
    }
    .sink(receiveCompletion: { completion in
        print("结束了")
        switch completion {
        case .finished:
            print("完成")
        case .failure(let error):
            print("错误:\(error.localizedDescription)")
        }
    }, receiveValue: { someValue in
        print("someValue: \(someValue)")
    })
    .store(in: &cancellables)

打印结果:

someValue: 1
someValue: 2
结束了
完成

output

output可精确地控制哪个位置上的数据可以输出,它可接收一个range如.output(at: 2...5)

_ = [1, 2, 3, 4, 5, 6, 7]
    .publisher
    .output(at: 4)
    .sink(receiveCompletion: { completion in
       ...
    }, receiveValue: { someValue in
        print("someValue: \(someValue)")
    })

Made with in Shangrao,China By 老雷

Copyright © devler.cn 1987 - Present

赣ICP备19009883号-1