Skip to content

Confusing, and possibly wrong wording In Concurrency: Tasks and Task Groups section. #363

@theundergroundsorcerer

Description

@theundergroundsorcerer

Location

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/concurrency#Tasks-and-Task-Groups

Description

In concurrency chapter, the wording in the Tasks and Task Groups after the second example (before the end of the section) states:

Like the previous example, this example creates a child task for each photo to download it. Unlike the previous example, the for-await-in loop waits for the next child task to finish, appends the result of that task to the array of results, and then continues waiting until all child tasks have finished. Finally, the task group returns the array of downloaded photos as its overall result.

The emphasized part suggests that there is a difference in the way the behavior for-await-in loops of in these two examples.

await withTaskGroup(of: Data.self) { group in
    let photoNames = await listPhotos(inGallery: "Summer Vacation")
    for name in photoNames {
        group.addTask {
            return await downloadPhoto(named: name)
        }
    }


    for await photo in group {
        show(photo)
    }
}

and second example

let photos = await withTaskGroup(of: Data.self) { group in
    let photoNames = await listPhotos(inGallery: "Summer Vacation")
    for name in photoNames {
        group.addTask {
            return await downloadPhoto(named: name)
        }
    }


    var results: [Data] = []
    for await photo in group {
        results.append(photo)
    }


    return results
}

From testing it seems there is not - in first example they are printed in the order they are downloaded and in second example they are appended in the order they are downloaded.

Correction

Can this paragraph be changed? (I have no suggestion for fix, as I fail to understand what it means). If these part is wrong - it would be nice to explain the original meaning.

If the emphasized part is correct, and I am wrong, namely there is a difference in the behavior of for-await-in loops in two examples - could more detail be provided how they can differ, why they differ and when these occurs? As it seems they essentially do the same. (just different actions on the same result in the task group in the same order the tasks are finished).


A general remark about the chapter - adding some examples one can immediately run and play with (or a repo with some examples) would be a very welcome addition. For now, the easiest way to emulate them is to write some tasks that sleep and return strings. Concurrency can be a confusing and seeing how actual code works is crucial for understanding.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions