Beginning Networking with URLSession, Episode 2: Introduction to Modern Concurrency | Kodeco, the new

Introduction to the modern concurrency features of Swift.

This is a companion discussion topic for the original entry at

Given the first part of the playground in this episode I am finding that it ignores the let sum statement, or seems to:

let task = Task {
  print("This is first.")

  let sum = (1...100000).reduce(0, +)
  try Task.checkCancellation()
  print("This is second: 1 + 2 + 3 + ... 100 = \(sum)")

print("This is last")

I never see the value of sum… but if I change the upper limit to 10000 then I do… what am I missing?
see below:

Also the next part of the video’s code does not seem to work… I am going to stop this lesson until I can get some guidance…see below:

same with the first one, dropping a 0 or two off of the try/await will make it work… its like it has issues with the longer times. Same thing by running the final playground which is untouched by me…

I cannot reproduce what you are experiencing, and I can use 1_000_000_000 through 20_000_000_000 without issue. The final project, untouched, will show the documentsDirectoryURL, which it does on mine.

Not much help I can offer except maybe close and restart the Playground and Xcode.

@ericjenkinson i did both… no change… in the morning I’ll create an unrelated playground and try again… maybe so wing is going on with my Xcode since I had to replace that project earlier… Thank you sir!!

So today, with the help of @ericjenkinson and @robertomachorro we found that Playgrounds has some peculiar behavior with async calls. I found that the code will work every time if you execute the playground manually, but if you have playground set to run Automatically, it will typically fail, but not always. I found this to be true as I varied the number in the Task.sleep(nanoseconds: method to see if maybe I was reaching a numeric limit of time for some reason. I thought I found a limit…and once I found it, I later put in a number that worked previously and it didnt work. That is when I found that it was not the number but how Playgrounds was responding to the Task… Robert experimented with it by putting in some code to run another await thread after the initial Task which then revealed the first task was being interrupted…

So I hope this helps someone in the future!

1 Like

Thanks for your comments :slight_smile:

Indeed playgrounds can be a bit finicky and result in inconsistencies despite your code being correct. I’ll add notes to this video in order to help guide others in the future.

Thank you sir! I am enjoying your course!!