Swift Apprentice v4.0.1

Hello,

I am working on Chapter 23 source code, “23-asynchronous-closures-and-memory-management”, and find a bug in line 127 that doesn’t print the sum of the numbers in the console. I’m using macOS 10.14.1; Xcode 10.1; Deployment target 10.14

execute(backgroundWork: { addNumbers(upTo: 100) },
mainWork: { log(message: “The sum is ($0)”) })

Thank you.

Don

@donwlarson Do you still have this issue?

I just ran the source code again. In reviewing the Xcode console, this is the output when the program completes. At no time did I see this line of text appear: "The sum is "

Memory management by Cosmin

Goodbye Editor Ray!

Goodbye Author Cosmin!

Goodbye Tutorial Memory management!

1

0

0

Goodbye Editor Ray!

Background thread: Adding numbers…

Goodbye Author Cosmin!

Program ended with exit code: 0

The problem with using the command line application is that you are the main thread. You have not started the dispatcher to handle any new code blocks on the dispatch queue. Ideally you would make a call to handle any blocks in the queue and then return to you but I have not figured any way to do this. You can call

dispatchMain()

at the end of all the code. This function never returns. Curiously the mainWork: code block log message claims it is not the main thread.

Here is the end of the new code, notice that we now have to explicitly call exit(0)

execute(backgroundWork: { addNumbers(upTo: 100) },
mainWork: { log(message: “The sum is ($0)”)
exit(0) } )

dispatchMain()

1 Like

Thank you, rlegault!

Don