这片主要介绍Combine基本用法

 1. Just

    let myPublisher = Just("1")
    
    let myScriber = Subscribers.Sink<String, Never> { completion in
        if completion == .finished {
            print("myScriber1 Finished")
        } else {
            print("myScriber1 Failure")
        }
    } receiveValue: { value in
        print(value)
    }

    myPublisher.subscribe(myScriber)


    打印结果:
    // 1
    // myScriber1 Finished

1. Just发布一个Publisher,just只会发送一次,发送完成结束本次订阅
2. Sink订阅者,订阅了Just,这个Sink后面是个泛型

        第一参数参数类型方法Just发送的是String类型,所以咱们观察者类型要保持一致

        第二参数是错误类型例子中参数是Never,就是不会报错,下面的例子中会介绍其它的
3.  Sink中有两个闭包 (receiveCompletionreceiveValue)

        receiveCompletion: 结束观察订阅
        receiveValue: 订阅结果

extension Publisher { 
    public func sink(receiveCompletion: @escaping ((Subscribers.Completion<Self.Failure&gt;) -&gt; Void), receiveValue: @escaping ((Self.Output) -> Void)) -> AnyCancellable
}

extension Publisher where Self.Failure == Never {
    public func sink(receiveValue: @escaping ((Self.Output) -> Void)) -> AnyCancellable
}

// 如果不需要receiveCompletion我们可以用Self.Failure == Never方法

2. Publishers

Publishers处于pipline的最上游,它的主要作用发送数据

    let arr = 0...3
    arr.publisher.sink { value in
        print(value)
    }

    打印结果:
    // 0
    // 1
    // 2
    // 3

Combine给了我们一个简便的写法

适用环境比如我们在意的是直接拿到结果,不需要中间的Operator
 

3. assign

    class Person {
        var age: Int = 0 {
            didSet {
                print("age: (age)")
            }
        }
    }
    
    
    let person = Person()
    let range = 0...2
    range.publisher.assign(to: .age, on: person)

    打印结果:
    // 0
    // 1
    // 2

keyPath

将发布者中的每个元素分配对象上的属性

使用Publisher/assign(to:on:)订阅者,当你想在每次发行者产生一个值时设置给定属性

在这个例子中,Publisher/assign(to:on:) 在person实例设置age属性的值

第二种用法

    class Person {
        @Published var age: Int = 0
    }
    
    
    let person = Person()
    Just(1).assign(to: &amp;person.$age)
    print(person.age) 

    打印结果: 
    // 1

熟悉SwiftUI的朋友看起来比较喜欢,属性包装器的写法

** assign 都是 Self.Failure == Never

4. Future

    class Person {
        var age: Int = 0
    }
    
    let futurePubliser = Future<Int ,Never> { promise in
        print("异步操作")
        promise(.success(2))
        
    }
    
    let subscribers = Subscribers.Sink<Int,Never> { completion in
        if completion == .finished {
            print("Finished")
        } else {
            print("Failure")
        }
    } receiveValue: { value in
        print(value)
    }

    futurePubliser.subscribe(subscribers) 
    
    let person = Person()
    futurePubliser.assign(to: .age, on: person)

    打印结果:
    // 异步操作
    // 2
    // Finished

Future和Just类似,区别在于:

        Just直接发送参数, Failure = Never
        Futrre是调用一个异步闭包,可以选择闭包结束发送数据, Failure = Error

5. Operator

    let myPubliser2 = Just("1")
    
    let tansValue = Publishers.Map<Just<String>, Int>(upstream: myPubliser2) { value in
        return Int(value) ?? 0
    }
    
    let myScriber2 = Subscribers.Sink<Int,Never>  { completion in
        if completion == .finished {
            print("Finished")
        } else {
            print("Failure")
        }
    } receiveValue: { value in
        print(value)
    }

    tansValue.subscribe(myScriber2)

    打印结果:
    // 1
    // Finished

使用提供的闭包转换来自上游发布者的所有元素的发布者

Operator处于pipline的中游,它的主要作用接收数据处理数据,再发送数据

此发布者可能发布的错误类型, 此发布者使用其上游发布者的失败类型

6. Operator2

    _ = Just(1).filter({ value in
        return value < 10
    }).sink(receiveCompletion: { completion in
        if completion == .finished {
            print("Finished")
        } else {
            print("Failure")
        }
    }, receiveValue: { value in
        print(value)
    })

    打印结果:
    // 1
    // Finished
extension Just {
    public func filter(_ isIncluded: (Output) -> Bool) -> Optional<Output>.Publisher
}

注意这个filter,它不是高阶函数里的那个filter,虽然它们功能都是数据过滤。但它属于Just, 它返回的是Publisher

7. CurrentValueSubjecsend

    // 定义一个错误类型
    enum myErrors: Error {
        case wrongValue
    }

    // 定义一个publiser
    let publiser = CurrentValueSubject<String, myErrors>("10")
    

    // 第一个接收者
    publiser.filter {
        return $0.count < 2
    }.sink { completion in
        if completion == .failure(myErrors.wrongValue) {
            print("Scriber1 Finished")
        } else {
            print("Scriber1: (completion)")
        }
    } receiveValue: { value in
        print("Scriber1: (value)")
    }
    
    // 第二个接收者
    let myScriber = Subscribers.Sink<String,myErrors> { completion in
        if completion == .failure(myErrors.wrongValue) {
            print("Scriber2 Finished")
        } else {
            print("Scriber2: (completion)")
        }
    } receiveValue: { value in
        print("Scriber2: (value)")
    }
    
    publiser.subscribe(myScriber)
    
    
    // 开始发送数据
    publiser.send("1")
    publiser.send("11")
    publiser.send(completion: .failure(.wrongValue))
    publiser.send("over")

    打印结果:
    // Scriber2: 10
    // Scriber1: 1
    // Scriber2: 1
    // Scriber2: 11
    // Scriber1 Finished
    // Scriber2 Finished

CurrentValueSubject可以订阅一个publiser, 它可以多点用send发布数据,直到发送completion结束观察

Failure = Error

8. CurrentValueSubject  Operator

    let subject1: CurrentValueSubject<Int,myErrors> = .init(10)
    let subject2: CurrentValueSubject<Int,myErrors> = .init(20)

    // 数据流 subject1 -> subject2
    _ = subject1.subscribe(subject2)

    let myScriber = Subscribers.Sink<Int,myErrors> { completion in
        if completion == .finished {
            print("Finished")
        } else {
            print("Failure")
        }
    } receiveValue: { value in
        print(value)
    }
    
    subject2.receive(subscriber: myScriber)
    
    subject1.send(1)
    subject1.send(2)
    subject2.send(3)
    subject1.send(completion: .failure(.wrongValue))
    subject1.send(11)
    subject2.send(12)

    打印结果:
    // 10
    // 1
    // 2
    // 3
    // Failure

CurrentValueSubject也可以当作Operator适用
例子中subject1处于pipline的上游,subject2处于pipline的中游

Failure = Error

subject1.send(1)      //  subject1 -> subject2 ->Scriber
subject1.send(2)      //  subject1 -> subject2 ->Scriber
subject2.send(3)      //  subject2 ->Scriber
subject1.send(completion: .failure(.wrongValue))   //  结束myScriber的响应者

9. PassthroughSubject

    let subject1: PassthroughSubject<Int,myErrors> = PassthroughSubject()
    let subject2: PassthroughSubject<Int,myErrors> = PassthroughSubject()

    // 数据流 subject1 -> subject2
    let cancelable = subject1.subscribe(subject2)

    let myScriber = Subscribers.Sink<Int,myErrors> { completion in
        if completion == .finished {
            print("Finished")
        } else {
            print("Failure")
        }
    } receiveValue: { value in
        print(value)
    }
    
    subject2.receive(subscriber: myScriber)
    
    subject1.send(1)
    subject1.send(2)
    subject2.send(3)
    subject1.send(completion: .failure(.wrongValue))
    subject1.send(11)
    subject2.send(12)

    打印结果: 
    // 1
    // 2
    // 3
    // Failure

PassthroughSubject和CurrentValueSubject功能类似

区别在于

        CurrentValueSubject: 有个初始化

        PassthroughSubject:没有初始化

原文地址:https://blog.csdn.net/u010130947/article/details/126847697

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_27998.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注