Skip to content
DeveloperMemos

Combine's CurrentValueSubject Explained

Combine, CurrentValueSubject, Swift1 min read

Combine's CurrentValueSubject is a powerful class provided by Apple's Combine framework, which is widely used in iOS development to handle reactive programming and asynchronous operations. In this article, we will explore the CurrentValueSubject class and understand how it can be utilized in your iOS applications.

Introduction to CurrentValueSubject

CurrentValueSubject is a type of subject in Combine that acts as both a publisher and a subscriber. It holds a current value and publishes any changes to its subscribers. It's similar to PassthroughSubject, but with an additional feature of holding a current value.

To create a CurrentValueSubject, you need to specify the initial value. For example, in Swift, you can create a CurrentValueSubject with an initial value of 0 like this:

1import Combine
2
3let subject = CurrentValueSubject<Int, Never>(0)

In the above code, we created a CurrentValueSubject of type Int with an initial value of 0. The second type parameter, Never, represents the absence of an error type.

Publishing and Subscribing to CurrentValueSubject

CurrentValueSubject provides two important methods: send(_:) and subscribe(_:). The send(_:) method allows you to update the current value and emit it to all subscribers, while the subscribe(_:) method allows you to subscribe to the subject and receive the current value and any subsequent changes.

Let's see an example where we publish and subscribe to a CurrentValueSubject:

1import Combine
2
3let subject = CurrentValueSubject<String, Never>("Hello")
4
5// Subscribing to the subject
6let subscription = subject
7 .sink { value in
8 print("Received value:", value)
9 }
10
11// Updating the value
12subject.send("Hello, World!")
13
14// Output: Received value: Hello, World!

In the above code, we created a CurrentValueSubject with an initial value of "Hello". Then we subscribed to the subject using the sink operator, which prints any received values. Finally, we updated the value using the send(_:) method, which triggers the subscription's closure and prints the new value.

Updating the Current Value

In addition to the initial value, you can update the current value of a CurrentValueSubject at any time. Let's take a look at an example:

1import Combine
2
3let subject = CurrentValueSubject<Int, Never>(10)
4
5subject.send(20)
6
7print("Current value:", subject.value)
8
9// Output: Current value: 20

In the above code, we created a CurrentValueSubject of type Int with an initial value of 10. Then we updated the value to 20 using the send(_:) method. Finally, we accessed the current value using the value property, which outputs 20.

In Summary

In this article, we explored the CurrentValueSubject class provided by the Combine framework in iOS development. We learned how to create a CurrentValueSubject with an initial value, publish changes to its subscribers using the send(_:) method, and subscribe to the subject to receive the current value and subsequent updates.