Aller au contenu

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.

for id in ids {
results.append(try await fetch(id))
}
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.