繁体   English   中英

什么是 PassthroughSubject 和 CurrentValueSubject

[英]What is PassthroughSubject & CurrentValueSubject

我碰巧研究了 Apple 新的 Combine 框架,在那里我看到了两件事

PassthroughSubject<String, Failure>

CurrentValueSubject<String, Failure>

有人可以向我解释它们的含义和用途吗?

我认为我们可以与现实世界的案例进行类比。

PassthroughSubject = 门铃按钮

当有人敲门时,只有您在家时才会收到通知(您是订阅者)

PassthroughSubject 没有状态,它将接收到的任何内容发送给订阅者。

CurrentValueSubject = 电灯开关当您外出时,有人打开您家中的灯。 你回到家,你知道有人打开了它们。

CurrentValueSubject 有一个初始状态,它保留你放入的数据作为它的状态。

PassthroughSubjectCurrentValueSubject都是符合Subject协议的发布者,这意味着您可以在它们上调用send以随意将新值推送到下游。

主要区别在于CurrentValueSubject具有状态感(当前值),而PassthroughSubject只是将值直接传递给其订阅者,而不记住“当前”值:

var current = CurrentValueSubject<Int, Never>(10)
var passthrough = PassthroughSubject<Int, Never>()

current.send(1)
passthrough.send(1)

current.sink(receiveValue: { print($0) })
passthrough.sink(receiveValue: { print($0) })

您会看到current.sink立即用1调用。 passthrough.sink没有被调用,因为它没有当前值。 只有在您订阅后发出的值才会调用接收器。

请注意,您还可以使用其value属性获取和设置CurrentValueSubject的当前值:

current.value // 1
current.value = 5 // equivalent to current.send(5)

这对于直通主题是不可能的。

PassthroughSubject用于表示事件。 将它用于按钮点击等事件。

CurrentValueSubject用于表示状态。 使用它来存储任何值,例如开关状态为关闭和打开。

注意: @PublishedCurrentValueSubject的一种。

PassthroughSubjectCurrentValueSubject都是Publisher —— 一种由 Combine 引入的类型——你可以订阅它们(当值可用时对值执行操作)。

它们都旨在使转换为使用组合范式变得容易。 它们都有一个值和一个错误类型,您可以向它们“发送”值(使所有订阅者都可以使用这些值)

我看到的两者之间的主要区别是CurrentValueSubject以一个值开头,而PassthroughSubject不是。 PassthroughSubject在概念上似乎更容易掌握,至少对我来说是这样。

PassthroughSubject可以很容易地用来代替委托模式,或者将现有的委托模式转换为组合。

//Replacing the delegate pattern
class MyType {
    let publisher: PassthroughSubject<String, Never> = PassthroughSubject()

    func doSomething() {
        //do whatever this class does

        //instead of this:
        //self.delegate?.handleValue(value)

        //do this:
        publisher.send(value)
    }
}

//Converting delegate pattern to Combine
class MyDel: SomeTypeDelegate {
    let publisher: PassthroughSubject<String, Never> = PassthroughSubject()

    func handle(_ value: String) {
        publisher.send(value)
    }
}

这两个示例都使用String作为值的类型,而它可以是任何值。

希望这可以帮助!

PassthroughSubject适用于像点击动作这样的事件

CurrentValueSubject适用于状态

在这个线程中已经发布了很多好的答案,只是想为使用 RxSwift 的人添加这个答案。

PassThroughSubject就像PublishSubject一样,它向订阅者广播一个事件,可能带有一些传递的值。

CurrentValueSubject类似于BehaviorRelay ,其中单个值与主题实例一起持久保存并在事件广播期间传递。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM