Swift Combine — ‘JUST’ Publisher
In the previous article, we have learnt about how to create a subscription using publisher and subscriber with notification centre example. If you wish to revise feel free to revisit the learning series.
‘Unlimited Demand’ — As its name suggests, SINK subscriber follows rule of unlimited demand. Whenever any publisher emits any event ‘SINK’ subscriber will always receive it unless subscription is cancelled or completed.
Many times we face the scenario where we only want to convey single event and our work for that task is done. e.g. We are developing a chatting application and we have a service where we want to convey either sent message is ‘read’ by the receiver or not. This situation demand a Publisher which emits only single event and its task is done. ‘JUST’ publisher from combine exactly provides this behaviour.
Let’s check how ‘JUST’ publisher works by some coding. Copy paste below skeleton code with ‘Just Publisher’ title.
import Foundation
import Combine
/*
This is a Util function for logs.
Function will take a title of the topic and will execute the blocks
after printing.
*/
public func myLearningCode(of title: String,
execute: () -> Void) {
print("\n ***** Example of:", title, "***** \n")
execute()
}
myLearningCode(of: "Just Publisher") {
}
Add below code lines for Publisher and subscriber.
myLearningCode(of: "Just Publisher") {
// 1
let singleEventPublisher = Just(" ** Event from 'Just' publisher **")
// 2
_ = singleEventPublisher.sink { completion in
// 4
print("Received completion \(completion) \n")
} receiveValue: { receivedValue in
// 3
print("Received a value \(receivedValue)")
}
}
Try to run and see what is the output.
Output
Let’s decode the working of this code.
- We are creating a ‘Just’ publisher by providing a event with a String value.
- Using our created publisher, we are using ‘sink’ subscriber to create a subscription. We are using ‘receiveValue’ as well as ‘completion’ blocks from ‘sink’ subscriber.
- When publisher will publish an event, we will get callback here and we have put a print statement for the reference.
- When publisher will send a completion, we will get callback in this closure block.
Remember, ‘JUST’ publisher will emit event for any subscriber only once and then completion will be sent. If same publisher emits other event for the different subscriber, older one will not get any event.
Let’s try to add one more subscription in our code and see.
myLearningCode(of: "Just Publisher") {
// 5
_ = singleEventPublisher.sink { completion in
// 7
print("Received completion \(completion) for subscription2 \n")
} receiveValue: { receivedValue in
// 6
print("** Received a value \(receivedValue) for subscription2 **")
}
}
5. Apart from our older subscription at Step #2, we are creating a new subscription using a ‘sink’ subscriber.
6. Received value block for the newer subscription (Step #5)
7. Completion event block for the newer subscription (Step #5)
At this point, our whole code is having a Publisher and two subscribers. Take a minute pause and think what should be the our output ! ! !
Full Code
myLearningCode(of: "Just Publisher") {
// 1
let singleEventPublisher = Just(" ** Event from 'Just' publisher **")
// 2
_ = singleEventPublisher.sink { completion in
// 4
print("Received completion \(completion) \n")
} receiveValue: { receivedValue in
// 3
print("Received a value \(receivedValue)")
}
// 5
_ = singleEventPublisher.sink { completion in
// 7
print("Received completion \(completion) for subscription2 \n")
} receiveValue: { receivedValue in
// 6
print("** Received a value \(receivedValue) for subscription2 **")
}
}
Output
If you see output, both subscribers have received only one event from the publisher.
Ideally, after the creation of our second subscription at step number #5, our first subscription should also receive events from the publisher as it is still not cancelled. This is because of the Publisher type being ‘JUST’, so it is sending event and a completion block only once per subscriber.
Summary
we have seen how ‘JUST’ publisher works with different subscriptions. I hope, this clears the nature of ‘JUST’ publisher. That’s it for the article. Remember the silver line for the ‘JUST’ publisher : “It will send event only once per subscriber followed by the completion”
We have seen ‘JUST’ publisher working, we will learn ‘ASSIGN’ subscriber in the next article. Stay tuned with the combine learning series.
Feel free to follow me to stay updated with the upcoming articles.
Thank You. Cheers!
#Pre-Requisite
Intermediate level of the swift language / ios is required for this Combine Article series.
#Credits