# Chapter 4 - Babble Timer countdown(to:) in Xcode 14 (Swift 5.7)

**URL:** <https://forums.kodeco.com/t/chapter-4-babble-timer-countdown-to-in-xcode-14-swift-5-7/174888>\
**Category:** Modern Concurrency in Swift\
**Created:** [July 14, 2022, 5:30am UTC](https://forums.kodeco.com/t/chapter-4-babble-timer-countdown-to-in-xcode-14-swift-5-7/174888 "2022-07-14T05:30:12Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter.suwara](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/peter.suwara/32/109793_2.png) [@peter.suwara](https://forums.kodeco.com/u/peter.suwara)\
**Post date:** [July 14, 2022, 5:30am UTC](https://forums.kodeco.com/t/chapter-4-babble-timer-countdown-to-in-xcode-14-swift-5-7/174888/1 "2022-07-14T05:30:12Z")

</div>

The countdown timer never fires.

Adding @MainActor to the function fixes the problem.

Full code:

```auto
  @MainActor
  func countdown(to message: String) async throws {
    guard !message.isEmpty else { return }
    var timer: Timer? // Do we need a reference to fire, surely not (test code)

    let counter = AsyncStream<String> { continuation in
      var countdown = 3
      Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
        continuation.yield("\(countdown) ...")
        countdown -= 1

        // FAILURE ETC ...
      }
    }

    for await countdownMessage in counter {
      try await say(countdownMessage)
    }
  }

```

---

<div class="post-metadata">

**Author:** ![robertomachorro](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/robertomachorro/32/111603_2.png) [@robertomachorro](https://forums.kodeco.com/u/robertomachorro)\
**Post date:** [July 18, 2022, 11:40am UTC](https://forums.kodeco.com/t/chapter-4-babble-timer-countdown-to-in-xcode-14-swift-5-7/174888/2 "2022-07-18T11:40:02Z")

</div>

@icanzilb code errata?

---

<div class="post-metadata">

**Author:** ![icanzilb](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/icanzilb/32/109333_2.png) [@icanzilb](https://forums.kodeco.com/u/icanzilb)\
**Post date:** [July 18, 2022, 12:31pm UTC](https://forums.kodeco.com/t/chapter-4-babble-timer-countdown-to-in-xcode-14-swift-5-7/174888/3 "2022-07-18T12:31:40Z")

</div>

Hey @robertomachorro - Xcode 14 is still in beta, we usually don’t support pre-release versions

---

<div class="post-metadata">

**Author:** ![robertomachorro](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/robertomachorro/32/111603_2.png) [@robertomachorro](https://forums.kodeco.com/u/robertomachorro)\
**Post date:** [July 19, 2022, 12:30pm UTC](https://forums.kodeco.com/t/chapter-4-babble-timer-countdown-to-in-xcode-14-swift-5-7/174888/4 "2022-07-19T12:30:21Z")

</div>

@peter.suwara as you can see from the author’s response above, beta versions of Xcode/Swift are understandably not supported. Beta versions may contain changes and incompatibilities, they may also change over time and create a moving target for updates. Xcode can run both release and beta versions in the system, please use the current release.

---

<div class="post-metadata">

**Author:** ![peter.suwara](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/peter.suwara/32/109793_2.png) [@peter.suwara](https://forums.kodeco.com/u/peter.suwara)\
**Post date:** [July 19, 2022, 10:15pm UTC](https://forums.kodeco.com/t/chapter-4-babble-timer-countdown-to-in-xcode-14-swift-5-7/174888/5 "2022-07-19T22:15:49Z")

</div>

Ok, thanks for the update guys.

---

<div class="post-metadata">

**Author:** ![peter.suwara](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/peter.suwara/32/109793_2.png) [@peter.suwara](https://forums.kodeco.com/u/peter.suwara)\
**Post date:** [July 19, 2022, 10:16pm UTC](https://forums.kodeco.com/t/chapter-4-babble-timer-countdown-to-in-xcode-14-swift-5-7/174888/6 "2022-07-19T22:16:19Z")

</div>

Thanks Robert. Also, how do I add an avatar picture to my profile?

---

<div class="post-metadata">

**Author:** ![robertomachorro](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/robertomachorro/32/111603_2.png) [@robertomachorro](https://forums.kodeco.com/u/robertomachorro)\
**Post date:** [July 20, 2022, 2:31pm UTC](https://forums.kodeco.com/t/chapter-4-babble-timer-countdown-to-in-xcode-14-swift-5-7/174888/7 "2022-07-20T14:31:53Z")

</div>

I believe that this is picked up from your Gravatar:

[https://en.gravatar.com](https://en.gravatar.com)

---

<div class="post-metadata">

**Author:** ![peter.suwara](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/peter.suwara/32/109793_2.png) [@peter.suwara](https://forums.kodeco.com/u/peter.suwara)\
**Post date:** [July 20, 2022, 11:38pm UTC](https://forums.kodeco.com/t/chapter-4-babble-timer-countdown-to-in-xcode-14-swift-5-7/174888/8 "2022-07-20T23:38:16Z")

</div>

Thanks @robertomachorro, I’ve updated my Gravatar 😃

---

<div class="post-metadata">

**Author:** ![peter.suwara](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/peter.suwara/32/109793_2.png) [@peter.suwara](https://forums.kodeco.com/u/peter.suwara)\
**Post date:** [August 26, 2022, 12:37am UTC](https://forums.kodeco.com/t/chapter-4-babble-timer-countdown-to-in-xcode-14-swift-5-7/174888/9 "2022-08-26T00:37:00Z")

</div>

Seems like things will break in Swift 6. We shouldn’t be holding a reference to async code like this. I take it you know about this issue @icanzilb ? :

![image](https://files.chunter.kodeco.com/original/3X/1/1/1145d1d3d73810c074e6c14fc5639ff7b5eb16ff.png)

---

<div class="post-metadata">

**Author:** ![icanzilb](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/icanzilb/32/109333_2.png) [@icanzilb](https://forums.kodeco.com/u/icanzilb)\
**Post date:** [September 1, 2022, 8:19am UTC](https://forums.kodeco.com/t/chapter-4-babble-timer-countdown-to-in-xcode-14-swift-5-7/174888/10 "2022-09-01T08:19:35Z")

</div>

Sure Peter, I do know about this. RayWenderlich books receive updates, those are usually on a yearly basis — each edition targets a given Xcode/Swift version. If you open the book you will see it targets Xcode 13 and Swift 5.5 because these were the latest versions at the time this edition was produced. Any future editions will support the latest versions at the time those are made. Obviously since these new warnings were introduced, future editions will feature different code in the chapter you are referring to. I hope that helps

---

<div class="post-metadata">

**Author:** ![kolya-j](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kolya-j/32/105896_2.png) [@kolya-j](https://forums.kodeco.com/u/kolya-j)\
**Post date:** [October 14, 2022, 2:56pm UTC](https://forums.kodeco.com/t/chapter-4-babble-timer-countdown-to-in-xcode-14-swift-5-7/174888/11 "2022-10-14T14:56:30Z")

</div>

Xcode 14 is not in Beta anymore. And if warnings are not a big issue, missed @MainActor from initial post is an issue which breaks app, because no runloop → no timer. No timer → No messages

---

<div class="post-metadata">

**Author:** ![robertomachorro](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/robertomachorro/32/111603_2.png) [@robertomachorro](https://forums.kodeco.com/u/robertomachorro)\
**Post date:** [October 15, 2022, 12:49pm UTC](https://forums.kodeco.com/t/chapter-4-babble-timer-countdown-to-in-xcode-14-swift-5-7/174888/12 "2022-10-15T12:49:20Z")

</div>

Hi @kolya-j ! Welcome to the Forum. Xcode was recently released and we are all exited about it. Having the expectation of books immediately after is tough, all books are updated continuously, but again, not immediate.

---

<div class="post-metadata">

**Author:** ![icanzilb](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/icanzilb/32/109333_2.png) [@icanzilb](https://forums.kodeco.com/u/icanzilb)\
**Post date:** [October 15, 2022, 4:05pm UTC](https://forums.kodeco.com/t/chapter-4-babble-timer-countdown-to-in-xcode-14-swift-5-7/174888/13 "2022-10-15T16:05:01Z")

</div>

Hi @kolya-j as Roberto noted if the book says the content targets Xcode 13 that’s the version that is supported. A later edition will be specifically suited for Xcode 14.

That said, I’m using Xcode 14.1 here and I don’t see an issue with Chapter 4. We’ve done a round of updates to address readers feedback lately, can you have a look if the issue you have is resolved? If not, could you post a more detailed description so we can assess if that’s something we should address adhoc before the next edition?

Thank you

---

<div class="post-metadata">

**Author:** ![kolya-j](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kolya-j/32/105896_2.png) [@kolya-j](https://forums.kodeco.com/u/kolya-j)\
**Post date:** [October 15, 2022, 5:34pm UTC](https://forums.kodeco.com/t/chapter-4-babble-timer-countdown-to-in-xcode-14-swift-5-7/174888/14 "2022-10-15T17:34:26Z")

</div>

I think I miss fact that book asks to use xcode 13.0. But now… thinking about code a bit more… It doesn’t matter which xcode you are using. Logical issue there not connected to xcode or iOS version.

What I did:

1. Got to [GitHub - raywenderlich/mcon-materials: The projects and materials that accompany the Modern Concurrency in Swift book](https://github.com/raywenderlich/mcon-materials)
2. See only one branch - editions 1.0, so I download code from there.
3. Open 04-async-streams/projects/final/Blabber.xcodeproj
4. Run server and run app
5. In chat use timer button to trigger countdown.

What should happen:  
We should start timer and timer called every second

What happen:  
Timer never triggered, therefore no messages send to server

Why it happens this way?

1. Button(action: ) takes sync block. When you start task inside sync code - no context exists, therefore Task starts on random queue… most importantly non-main. Even if initial clouser was called on main thread. I saw this issue on xcode 13 as well, one of first things I learned hard way.
2. If you call schedule timer from non-main thread - it is most likely won’t work, because timer works only inside of runloop. And no one spin runloop in any romdom queue
3. I attached screenshot confirmign my point about non-mina queue call, you can see it in debugger

I honeslty beleive issue was there on xcode 13, 14 and will be on 14.1. At least this is my currenctt understanding of asyc code, I’m still reading the book, but already have some experience with it.

And fix is really simple - you just need require main thread execution of countdown fucntion, by adding `@MainActor`.

![Screenshot 2022-10-15 at 18.21.23](https://files.chunter.kodeco.com/original/3X/1/4/1424969c6ba3c85f876683e7dc2287960102e35c.jpeg)

---

<div class="post-metadata">

**Author:** ![icanzilb](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/icanzilb/32/109333_2.png) [@icanzilb](https://forums.kodeco.com/u/icanzilb)\
**Post date:** [October 15, 2022, 6:56pm UTC](https://forums.kodeco.com/t/chapter-4-babble-timer-countdown-to-in-xcode-14-swift-5-7/174888/15 "2022-10-15T18:56:48Z")

</div>

@kolya-j I see, that was helpful — I didn’t realize we’re talking about the final project. Now I can reproduce the issue and we’ll fix it. This must’ve been introduced in a recent Swift version — I don’t believe it’s possible that all of the editors and 1000s of readers didn’t notice this before.

---

<div class="post-metadata">

**Author:** ![kolya-j](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kolya-j/32/105896_2.png) [@kolya-j](https://forums.kodeco.com/u/kolya-j)\
**Post date:** [October 15, 2022, 7:13pm UTC](https://forums.kodeco.com/t/chapter-4-babble-timer-countdown-to-in-xcode-14-swift-5-7/174888/16 "2022-10-15T19:13:51Z")

</div>

I downloded xcode 13 and I can confitm, that it indeed works in last xcode 13. Which is suprise for me.  
About fix, it also should be done in starter project because, `countdown` function exists in it already.

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![peter.suwara](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/peter.suwara/32/109793_2.png) [@peter.suwara](https://forums.kodeco.com/u/peter.suwara)\
**Post date:** [December 8, 2022, 10:38pm UTC](https://forums.kodeco.com/t/chapter-4-babble-timer-countdown-to-in-xcode-14-swift-5-7/174888/17 "2022-12-08T22:38:33Z")

</div>

Glad the issue has been resolved 🙂 nice work!

---

<div class="post-metadata">

**Author:** ![janeer](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/janeer/32/106494_2.png) [@janeer](https://forums.kodeco.com/u/janeer)\
**Post date:** [February 13, 2023, 5:48pm UTC](https://forums.kodeco.com/t/chapter-4-babble-timer-countdown-to-in-xcode-14-swift-5-7/174888/18 "2023-02-13T17:48:44Z")

</div>

Xcode is cool. But, how did you add your avatars? I can’t even with Gravatar))
