# Received Cancel in Chapter 2

**URL:** <https://forums.kodeco.com/t/received-cancel-in-chapter-2/91581>\
**Category:** Combine: Asynchronous Programming with Swift\
**Created:** [December 15, 2019, 11:51pm UTC](https://forums.kodeco.com/t/received-cancel-in-chapter-2/91581 "2019-12-15T23:51:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![velin92](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/velin92/32/81494_2.png) [@velin92](https://forums.kodeco.com/u/velin92)\
**Post date:** [December 15, 2019, 11:51pm UTC](https://forums.kodeco.com/t/received-cancel-in-chapter-2/91581/1 "2019-12-15T23:51:56Z")

</div>

Hello!  
I am actually following the Combine book, and it’s very interesting and detailed.  
However there is a weird behavior that is differing from the one explained in the book.

If I use the store(in:) function to store a subscription inside the subscriptions Set, the book says that at the end of the playground, since subscriptions is deallocated, automatically all contained subscriptions will be cancelled.  
To actually check this, the book suggests you to add the print() operatore before the sink.  
And the print() works indeed fine, showing all the “received” messages that the subscribers receive on their subscriptions.  
But on the book is also said that on the termination of the playground, since subscriptions is deallocated, the “received cancel” should be print by all the subscriptions contained in the set, if they where not completed.  
However this does not actually happen, and I even tested it by using the “final” playground provided in chapter.  
If instead I force a removeAll() on the subscriptions Set, it does actually work, and the “received cancel” is printed.

Is this a problem of playground? Or are actually the subscriptions cancelled, but the receive is not printed for this event?

Thanks.

---

<div class="post-metadata">

**Author:** ![yongwoo](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/yongwoo/32/81494_2.png) [@yongwoo](https://forums.kodeco.com/u/yongwoo)\
**Post date:** [December 22, 2019, 2:25am UTC](https://forums.kodeco.com/t/received-cancel-in-chapter-2/91581/2 "2019-12-22T02:25:09Z")

</div>

@velin92 Thank you for posting this question. I was just wondering the same thing.

---

<div class="post-metadata">

**Author:** ![mattwaller](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mattwaller/32/81494_2.png) [@mattwaller](https://forums.kodeco.com/u/mattwaller)\
**Post date:** [January 9, 2020, 5:13pm UTC](https://forums.kodeco.com/t/received-cancel-in-chapter-2/91581/3 "2020-01-09T17:13:26Z")

</div>

Yep. I have the same question about the lack of canceling without emptying the set.

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [January 13, 2020, 9:46pm UTC](https://forums.kodeco.com/t/received-cancel-in-chapter-2/91581/4 "2020-01-13T21:46:40Z")

</div>

@scotteg Can you please help with this when you get a chance? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![scotteg](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/scotteg/32/109788_2.png) [@scotteg](https://forums.kodeco.com/u/scotteg)\
**Post date:** [January 14, 2020, 2:29pm UTC](https://forums.kodeco.com/t/received-cancel-in-chapter-2/91581/5 "2020-01-14T14:29:32Z")

</div>

Hi @velin92, thanks for reporting this issue. Great catch!

Here’s what’s going on — there are two contributing factors:

1. The `subscriptions` set was moved to the global scope of the playground after I had completed this chapter. Why? We were battling issues with beta versions of Xcode 11 playgrounds, and at one point we issued a beta release of the book that had a workaround that _didn’t even use_ playgrounds. During those revisions the `subscriptions` set was moved to be defined globally vs. being defined in each `example(_:action:)` block. And we didn’t catch this issue in review.

2. At the point in the book when it is indicated that you’ll see `receive cancel` printed, you have not yet sent the `.finished` completion event. So, _if_ the `subscriptions` set was still defined in the `example` scope, the subscriptions would be canceled and you’d see `receive cancel` printed twice, as indicated in the text. In the next passage, you add sending a `.finished` event. So even if `subscriptions` was still defined in the `example` scope, you still would not see `receive cancel`, because the publisher is already terminated.

This revised code includes the local `subscriptions` set and it _will_ print the `receive cancel` events to the console:

```auto
example(of: "CurrentValueSubject") {
  var subscriptions = Set<AnyCancellable>()
    
  // 1
  let subject = CurrentValueSubject<Int, Never>(0)
  
  // 2
  subject
    .print()
    .sink(receiveValue: { print($0) })
    .store(in: &subscriptions) // 3
  
  subject.send(1)
  subject.send(2)
  
  print(subject.value)
  subject.value = 3
  print(subject.value)
  
  subject
    .print()
    .sink(receiveValue: { print("Second subscription:", $0) })
    .store(in: &subscriptions)
}

```

I will update the chapter to define a local `subscriptions` set in this example to restore showing how it will cancel the contained subscriptions when deinitialized.

Thanks again!

cc: @yongwoo @mattwaller

---

<div class="post-metadata">

**Author:** ![beefycode](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/beefycode/32/102075_2.png) [@beefycode](https://forums.kodeco.com/u/beefycode)\
**Post date:** [July 21, 2020, 10:50pm UTC](https://forums.kodeco.com/t/received-cancel-in-chapter-2/91581/6 "2020-07-21T22:50:49Z")

</div>

I’m using Xcode V11.6 (11E708) and even though I’ve been using auto complete which does indeed have the `.store(in: &subscriptions)` methods, like the answer mentioned above, when the playground is run the console still doesn’t have the `receive cancel` output like in the book. Just an FYI.

Does this mean that there is still a `Set<AnyCancellable>` with values in it located somewhere in a global scope of the playground?

Edit: using a `print(subscriptions)` at the end of the playground file at this point returned an array of `[Combine.AnyCancellable, Combine.AnyCancellable]` so I guess that’s a yes.

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [April 4, 2021, 2:56am UTC](https://forums.kodeco.com/t/received-cancel-in-chapter-2/91581/7 "2021-04-04T02:56:30Z")

</div>

@beefycode Thank you for sharing your solution - much appreciated!
