What is PassthroughSubject & CurrentValueSubject

23,761

Solution 1

I think we can make analogies with real world cases.

PassthroughSubject = A doorbell push button

When someone rings the door, you are notified only if you are at home (you are the subscriber)

PassthroughSubject doesn't have a state, it emits whatever it receives to its subscribers.

CurrentValueSubject = A light switch Someone turns on the lights in your home when you are outside. You get back home and you know someone has turned them on.

CurrentValueSubject has an initial state, it retains the data you put in as its state.

Solution 2

Both PassthroughSubject and CurrentValueSubject are publishers that conform to the Subject protocol which means you can call send on them to push new values downstream at will.

The main difference is that CurrentValueSubject has a sense of state (current value) and PassthroughSubject simply relays values directly to its subscribers without remembering the "current" value:

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) })

You'd see that the current.sink is called immediately with 1. The passthrough.sink is not called because it has no current value. The sink will only be called for values that are emitted after you subscribe.

Note that you can also get and set the current value of a CurrentValueSubject using its value property:

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

This isn't possible for a passthrough subject.

Solution 3

PassthroughSubject and CurrentValueSubject are both Publishers — a type introduced by Combine — that you can subscribe to (performing operations on values when values are available).

They both are designed to make it easy to transfer to using the Combine paradigm. They both have a value and an error type, and you can "send" values to them (making the values available to all subscribers)

The main difference between the two that I've seen is that CurrentValueSubject starts with a value, while PassthroughSubject does not. PassthroughSubject seems easier to grasp conceptually, at least for me.

PassthroughSubject can easily be used in place of a delegate pattern, or to convert an existing delegate pattern to Combine.

//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)
    }
}

Both of these examples use String as the type of the value, while it could be anything.

Hope this helps!

Solution 4

PassthroughSubject is used for representing events. Use it for events like button tap.

CurrentValueSubject is used representing state. Use it for storing any value, say state of switch as off and on.

Note: @Published is kind of CurrentValueSubject.

Solution 5

PassthroughSubject is suitable for event like tap action

CurrentValueSubject is suitable for state

Share:
23,761
Nasir
Author by

Nasir

Talks with Apple devices in Swift &amp; Objective-C

Updated on August 19, 2021

Comments

  • Nasir
    Nasir over 2 years

    I happen to look into Apple's new Combine framework, where I see two things

    PassthroughSubject<String, Failure>

    CurrentValueSubject<String, Failure>

    Can someone explain to me what is meaning & use of them?