# Combine: Getting Started | raywenderlich.com

**URL:** <https://forums.kodeco.com/t/combine-getting-started-raywenderlich-com/104251>\
**Category:** 🎓 Courses & Articles\
**Created:** [April 6, 2020, 1:35pm UTC](https://forums.kodeco.com/t/combine-getting-started-raywenderlich-com/104251 "2020-04-06T13:35:10Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![fbrancati](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/fbrancati/32/109968_2.png) [@fbrancati](https://forums.kodeco.com/u/fbrancati)\
**Post date:** [April 6, 2020, 1:35pm UTC](https://forums.kodeco.com/t/combine-getting-started-raywenderlich-com/104251/1 "2020-04-06T13:35:10Z")

</div>

Learn how to use Combine’s Publisher and Subscriber to handle event streams, merge multiple publishers and more.

* * *
This is a companion discussion topic for the original entry at [https://www.raywenderlich.com/7864801-combine-getting-started](https://www.raywenderlich.com/7864801-combine-getting-started)

---

<div class="post-metadata">

**Author:** ![giguerea](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/giguerea/32/81494_2.png) [@giguerea](https://forums.kodeco.com/u/giguerea)\
**Post date:** [April 10, 2020, 9:21pm UTC](https://forums.kodeco.com/t/combine-getting-started-raywenderlich-com/104251/2 "2020-04-10T21:21:15Z")

</div>

thanks, great article, just want to double I check I follow correctly, in stopGame by adding this

> subscriptions.forEach { $0.cancel() }

you don’t need anymore the second line ?

> gameTimer?.cancel()

---

<div class="post-metadata">

**Author:** ![fbrancati](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/fbrancati/32/109968_2.png) [@fbrancati](https://forums.kodeco.com/u/fbrancati)\
**Post date:** [April 11, 2020, 10:04am UTC](https://forums.kodeco.com/t/combine-getting-started-raywenderlich-com/104251/3 "2020-04-11T10:04:53Z")

</div>

Thanks!

The `gameTimer` property isn’t stored into the `subscriptions` `Set`, so you need to cancel it with `gameTimer?.cancel()` method.

By calling the `subscriptions.forEach { $0.cancel() }` method you will cancel all the subscriptions created for getting random images and downloading them.

---

<div class="post-metadata">

**Author:** ![giguerea](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/giguerea/32/81494_2.png) [@giguerea](https://forums.kodeco.com/u/giguerea)\
**Post date:** [April 11, 2020, 2:38pm UTC](https://forums.kodeco.com/t/combine-getting-started-raywenderlich-com/104251/4 "2020-04-11T14:38:15Z")

</div>

your right, sorry my mistake!

---

<div class="post-metadata">

**Author:** ![fbrancati](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/fbrancati/32/109968_2.png) [@fbrancati](https://forums.kodeco.com/u/fbrancati)\
**Post date:** [April 12, 2020, 10:33am UTC](https://forums.kodeco.com/t/combine-getting-started-raywenderlich-com/104251/5 "2020-04-12T10:33:37Z")

</div>

Don’t worry! You’re welcome 😉

---

<div class="post-metadata">

**Author:** ![ranbanramao](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ranbanramao/32/108537_2.png) [@ranbanramao](https://forums.kodeco.com/u/ranbanramao)\
**Post date:** [June 9, 2020, 9:40am UTC](https://forums.kodeco.com/t/combine-getting-started-raywenderlich-com/104251/6 "2020-06-09T09:40:20Z")

</div>

Handy article.

Could you please elaborate why the start of the closure expression within the below operator for the `download()` method is different to the `randomImage()` method?  
`.tryMap { response -> Data in`

Omission of `-> Data` results in a _Generic parameter ‘T’ could not be inferred_ error.  
If both the publishers from `URLSession` return (data: Data, response: URLResponse), why do they need to be processed differently?

---

<div class="post-metadata">

**Author:** ![fbrancati](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/fbrancati/32/109968_2.png) [@fbrancati](https://forums.kodeco.com/u/fbrancati)\
**Post date:** [June 11, 2020, 10:18pm UTC](https://forums.kodeco.com/t/combine-getting-started-raywenderlich-com/104251/7 "2020-06-11T22:18:47Z")

</div>

Both the `tryMap` operators on the `download()` and the `randomImage()` functions do the same things, can you please elaborate on it?

---

<div class="post-metadata">

**Author:** ![ranbanramao](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ranbanramao/32/108537_2.png) [@ranbanramao](https://forums.kodeco.com/u/ranbanramao)\
**Post date:** [June 22, 2020, 2:41am UTC](https://forums.kodeco.com/t/combine-getting-started-raywenderlich-com/104251/8 "2020-06-22T02:41:39Z")

</div>

From my understanding, you are explicitly specifying `Data` as being the return type in the closure to `tryMap` for the `download()` method, but are not doing to same for the `randomImage()` method. As you’ve stated, those two functions perform the same action, for which reason I was curious about the above discrepancy.

---

<div class="post-metadata">

**Author:** ![fbrancati](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/fbrancati/32/109968_2.png) [@fbrancati](https://forums.kodeco.com/u/fbrancati)\
**Post date:** [June 28, 2020, 1:54pm UTC](https://forums.kodeco.com/t/combine-getting-started-raywenderlich-com/104251/9 "2020-06-28T13:54:43Z")

</div>

Both the first `tryMap` on `donwload(url:)` and `randomImage(url:)` does the same thing. The second `tryMap` on `download(url:)` converts the received `Data` into a valid `UIImage`.

The reason is that if you add just one `tryMap` the compiler can infer the return type, otherwise not.  
For example, try to remove the return type on `randomImage(url:)`, in this case, you will not get any error because the compiler can infer the return type.  
Otherwise, if you try to remove the return type on the first `tryMap` on the `download(url:)` function you will get an error because the second `tryMap` needs to explicitly know what’s the input type.

---

<div class="post-metadata">

**Author:** ![chuckzhou](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/chuckzhou/32/103024_2.png) [@chuckzhou](https://forums.kodeco.com/u/chuckzhou)\
**Post date:** [July 5, 2020, 3:43am UTC](https://forums.kodeco.com/t/combine-getting-started-raywenderlich-com/104251/10 "2020-07-05T03:43:32Z")

</div>

This is what I want to ask too, thanks for the explanation.

---

<div class="post-metadata">

**Author:** ![chuckzhou](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/chuckzhou/32/103024_2.png) [@chuckzhou](https://forums.kodeco.com/u/chuckzhou)\
**Post date:** [July 6, 2020, 11:51am UTC](https://forums.kodeco.com/t/combine-getting-started-raywenderlich-com/104251/11 "2020-07-06T11:51:34Z")

</div>

In `Refining the App` section, would you mind to explain more about `.store(in: &subscriptions)` like what it does and why it is doing it. Doesn’t `AnyCancellable` would call the `.cancel()` automatically?

I don’t understand why we need to remove `subscribers` manually? We didn’t do that in `randomImage()` or `download()`. Because we used `Timer.publish()` this time, so we need to add the logic on `cancel()` manually, am I right?

---

<div class="post-metadata">

**Author:** ![ranbanramao](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ranbanramao/32/108537_2.png) [@ranbanramao](https://forums.kodeco.com/u/ranbanramao)\
**Post date:** [July 12, 2020, 7:43am UTC](https://forums.kodeco.com/t/combine-getting-started-raywenderlich-com/104251/12 "2020-07-12T07:43:11Z")

</div>

Right, that explains it. Thank you!

---

<div class="post-metadata">

**Author:** ![thegodland](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/thegodland/32/81494_2.png) [@thegodland](https://forums.kodeco.com/u/thegodland)\
**Post date:** [January 6, 2021, 3:10am UTC](https://forums.kodeco.com/t/combine-getting-started-raywenderlich-com/104251/13 "2021-01-06T03:10:30Z")

</div>

Hi Thank you for the article. When I played the game, I encountered many times which all four images are same (first, second images are randomly same) for this case, how do we identify in the completion handler and initial a new publisher? I am not sure how we can make it recreated one until it’s not the same

---

<div class="post-metadata">

**Author:** ![fbrancati](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/fbrancati/32/109968_2.png) [@fbrancati](https://forums.kodeco.com/u/fbrancati)\
**Post date:** [January 17, 2021, 4:35pm UTC](https://forums.kodeco.com/t/combine-getting-started-raywenderlich-com/104251/14 "2021-01-17T16:35:42Z")

</div>

Hi [thegodland](https://forums.kodeco.com/u/thegodland)! It happens sometimes, you can try with a `flatMap` to `throw` an error and to get a new `UIImage` if the first and the second images are equal.

---

<div class="post-metadata">

**Author:** ![shrutisharma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shrutisharma/32/81494_2.png) [@shrutisharma](https://forums.kodeco.com/u/shrutisharma)\
**Post date:** [January 29, 2021, 12:01am UTC](https://forums.kodeco.com/t/combine-getting-started-raywenderlich-com/104251/15 "2021-01-29T00:01:55Z")

</div>

Hi Fabrizio

Thank you for this wonderful article.  
I have a question here. I am able to understand how the Publisher Subscriber model works.  
But I am quite confused as to where are those methods depicted in the diagram like \* subscribe, receive(subscription:), receive(\_:), receive(completion:) implemented? I understand these are the methods of Publisher protocol. So does that mean the dataTaskPublisher or Timer Publisher classes would have those implementations.

Please clarify. Thank You.

---

<div class="post-metadata">

**Author:** ![fbrancati](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/fbrancati/32/109968_2.png) [@fbrancati](https://forums.kodeco.com/u/fbrancati)\
**Post date:** [February 21, 2021, 9:17pm UTC](https://forums.kodeco.com/t/combine-getting-started-raywenderlich-com/104251/16 "2021-02-21T21:17:56Z")

</div>

Hi @shrutisharma, those `dataTaskPublisher` and `TimerPublisher` are both `Publishers` so, yes they have `receive(subscription:)`, `receive(_:)`, `receive(completion:)` methods. We used other methods to get values from those `Publisher`s, like `tryMap`.  
We also used `sink(receiveCompletion:, receiveValue:)` that is called at every sent value and once the `Publisher` has finished sending values.
