Combine操作符Sequence示例


2天前 3次点击 来自 移动端

标签: Swift

Sequence 序列

Sequence 常用于对数据序列做各种规则取值

first/firstWhere/tryFirstWhere

获取序列第一个值后即停止接收数据,firstWhere闭包中可指定条件

["123", "3456", "56"]
    .publisher
    .first { value in
        value.count > 3
    }
    .sink { print($0) }

last/lastWhere/tryLastWhere

同上,略

dropFirst

函数名已表达的很清楚,可指定丢弃的数量.dropFirst(3)

dropUntilOutput

常用于指定triggerPublisher来触发当前的publisher,如下只用triggerPublisher触发之后的数据才有效

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

_ = publisher
    .drop(untilOutputFrom: triggerPublisher)
    .sink(receiveCompletion: { completion in
        print("Completed")
        switch completion {
        case .finished:
            print("Finished")
        case let .failure(error):
            print("Error:\(error.localizedDescription)")
        }
    }, receiveValue: { someValue in
        print("someValue: \(someValue)")
    })

publisher.send("123")
triggerPublisher.send(1)
publisher.send("456")
publisher.send(completion: Subscribers.Completion.finished)

dropWhile/tryDropWhile

丢弃所有不符合规则的数据后,会接收之后的所有数据

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

prepend

prepend常用于将2个publisher的数据流合在一起

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

_ = secondPublisher
    .prepend(firstPublisher)
    .sink(receiveCompletion: { completion in
        print("Completed")
        switch completion {
        case .finished:
            print("Finished")
        case let .failure(error):
            print("Error:\(error.localizedDescription)")
        }
    }, receiveValue: { someValue in
        print("someValue: \(someValue)")
    })

firstPublisher.send("123")
firstPublisher.send(completion: Subscribers.Completion.finished)
secondPublisher.send("456")
secondPublisher.send(completion: Subscribers.Completion.finished)

prefix/prefixWhile/tryPrefixWhile

为publisher增加限制条件,.prefix(3) 只接收3个数据,在闭包中指定接收的数据规则,功能上类似filter

[1, 2, 3, 4]
    .publisher
    .prefix { value -> Bool in
        value <= 2
    }
    .sink(receiveCompletion: { completion in
        print("Completed")
        switch completion {
        case .finished:
            print("Finished")
        case let .failure(error):
            print("Error:\(error.localizedDescription)")
        }
    }, receiveValue: { someValue in
        print("someValue: \(someValue)")
    })

此外同prefix(untilOutputFrom:)也可以接收类似triggerPublisher,triggerPublisher发送消息后不再接收数据

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

firstPublisher
    .prefix(untilOutputFrom: triggerPublisher)
    .sink(receiveCompletion: { completion in
        print("Completed")
        switch completion {
        case .finished:
            print("Finished")
        case let .failure(error):
            print("Error:\(error.localizedDescription)")
        }
    }, receiveValue: { someValue in
        print("someValue: \(someValue)")
    })

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

triggerPublisher.send(0)
firstPublisher.send(4)

output

output可以准确的获取数据流中的某个位置,或一个range


[1, 2, 3]
    .publisher
    .output(at: 2)
    .sink(receiveValue: { someValue in
        print("someValue: \(someValue)")
    })

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

Made with in Shangrao,China By Devler.

Copyright © Devler 2012 - 2022

赣ICP备19009883号-1