Await In Loop (ARCH9030)
Ce contenu n’est pas encore disponible dans votre langue.
Key swift.async.await-in-loop · Severity info · Category async
Flags await inside a for-in loop over independent items - the work is serialised. A while / repeat loop retries until something succeeds and cannot be parallelised.
Incorrect
Section titled “Incorrect”for id in ids { results.append(try await fetch(id))}Correct
Section titled “Correct”try await withThrowingTaskGroup(of: Item.self) { group in for id in ids { group.addTask { try await fetch(id) } } for try await item in group { results.append(item) }}Use a task group (or async let) to run the iterations concurrently.