AndyIbanez / andyibanez-com

Static website.
1 stars 0 forks source link

posts/structured-concurrency-in-swift-using-async-let/ #36

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

Structured Concurrency in Swift: Using async let • Andy Ibanez

https://www.andyibanez.com/posts/structured-concurrency-in-swift-using-async-let/

w-i-n-s commented 3 years ago

@andyibanez Thanks. Seems like unicorn will arrive to solve most of multitasking #painin*ss

ran-helfer commented 2 years ago

@AndyIbanez Thanks for this wonderful tutorial. Very much appreciated !

I have 2 questions for you - please :)

Question 1

Task.checkCancellation()

As you have mentioned,

whenever one of them throws an error, downloadImageAndMetadata will throw the same error

So how will it help us if we call:

Task.checkCancellation()

At the beginning of each method ?

Seem like the parallel downloading in this code is per one image. So we download the

Question 2

How come we can append such try await into an array of properties and compiler is cool with that ? It felt a bit scary since we might try to append nil into the array ?

imagesMetadata += [try await image]

Or are we saying that we can't append nil into the array since 'nil' won't happen and an error will result instead ?

AndyIbanez commented 2 years ago

@ran-helfer Question 1: Task.checkCancellation Will throw an error if another task up above it has been cancelled. We add it at the beginning to make sure we don’t do any unnecessary work if that’s case. These methods may be called concurrently by a task higher in the task tree. Because cancellation is cooperative, unless you check for cancellation, it’s possible they will try to download data even if they are cancelled and the result will be discarded if someone else was cancelled. That’s why we add the check the earliest that we can.

Question 2: that won’t be a problem because if the image download fails (try Throws an error), an error will be thrown before it adds to the array. In that case the appending operation will never happen.

ran-helfer commented 2 years ago

Awesome explanation ! Thank you very much for this series of articles. I continue to read it !

panviktor commented 1 year ago

Thank you! Greatest Article! Amazing!

shurale85 commented 1 year ago

First of all, thank you so much for series of great article. And u pls explain few moments. 1) if i am not mistaken downloadMetadata throws an exception that is propogated into downloadImageAndMetadata. Excetion throwing results into task that is related to downloadImageAndMetadata is cancelled. And child task that is related to downloadImage is marked as cancelled. Am I right. 2) How we can get info about Task of the currently executing method?

andrii-popov-jt commented 10 months ago

Thank you for the great series. "How are we gonna “lock” and guarantee access and that the final completion handler is called?"....am DispatchGroup maybe ?:))) Imho: the issue with async/await that its "supplementary routines" (yes yes async/await is not the end of the story but just a tip if the aisberg) is spread across scopes and sometimes different files and not totally "closures free". GCD is maybe not a linear but all the stuff mainly in one place...