Question about WWDC Video - Explore Structured Concurrency in Swift

Hello,

Watching the WWDC Video related to structured concurrency, the speaker, when discussing Task Groups, mentions that for each thumbnail in the loop we call fetchOneThumbnail to process it, which creates exactly two child tasks. My question is, how are two child tasks created, in this snippet of code:

for id in ids {  
   thumbnails[id] = try await fetchOneThumbnail(withID: id)
}

I thought that this would create a single child task, resulting from the try await fetchOneThumbanil(withID: id) expression, per id. What is the second child task that the speaker is referring to?

Thank you

IIUC the 'two child tasks' to which they are referring at that point in the presentation are those created by the async let bindings in the implementation of the fetchOneThumbnail(...) method. the sample code implementation looks like:

func fetchOneThumbnail(withID id: String) async throws -> UIImage {
    let imageReq = imageRequest(for: id), metadataReq = metadataRequest(for: id)
    async let (data, _) = URLSession.shared.data(for: imageReq)
    async let (metadata, _) = URLSession.shared.data(for: metadataReq)
    guard let size = parseSize(from: try await metadata),
          let image = try await UIImage(data: data)?.byPreparingThumbnail(ofSize: size)
    else {
        throw ThumbnailFailedError()
    }
    return image
}

and since this has two async lets, those correspond to the two child tasks.

2 Likes

This clears it up thanks!

1 Like