Swift Combine: CurrentValueSubject vs PassthroughSubject, commonalities and differences

It is shocking that quite a few developers who use combine do not know the difference between CurrentValueSubject and PassthroughSubject. You didn’t know? Don’t worry, the difference is quite obvious.

What is Subject?

PassthroughSubject, CurrentValueSubject

Publisher that you can continuously send new values down.

Subject in Combine Framework is a special kind. It’s a publisher, but you can send data continuously with subject.send() method. But CurrentValueSubject and PassthroughSubject is a little different.

Commonalities

[1] Subscribe to Subject

Both PassthroughSubject and CurrentValueSubject can subscribe

[2] Passing down new value with Subject

Passing down new value with Subject

[3] Sending completion finished with Subject

Sending completion finished with Subject

Difference

PassthroughSubject, CurrentValueSubject

The main difference between CurrentValueSubject and PassthroughSubject is that “CurrentValueSubject can hold a value but PassthroughSubject just sends data and never hold a value.”

What do I have to use between CurrentValueSubject and PassthroughSubject?

Using together

Most of the cases, you would use CurrentValueSubject and you should use PassthroughSubject for the event things.

Conclusion

  • Subject

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!