Skip to content

Instantly share code, notes, and snippets.

@erdemildiz
Created January 30, 2023 07:38
Show Gist options
  • Select an option

  • Save erdemildiz/5d1a133893233e5ab81d3ceec9e0316f to your computer and use it in GitHub Desktop.

Select an option

Save erdemildiz/5d1a133893233e5ab81d3ceec9e0316f to your computer and use it in GitHub Desktop.

Revisions

  1. erdemildiz revised this gist Jan 30, 2023. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions taskgroup.swift
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // Source: https://www.avanderlee.com/concurrency/task-groups-in-swift/

    let images = try await withThrowingTaskGroup(of: UIImage.self, returning: [UIImage].self) { taskGroup in
    let photoURLs = try await listPhotoURLs(inGallery: "Amsterdam Holiday")
    for photoURL in photoURLs {
  2. erdemildiz created this gist Jan 30, 2023.
    14 changes: 14 additions & 0 deletions taskgroup.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    let images = try await withThrowingTaskGroup(of: UIImage.self, returning: [UIImage].self) { taskGroup in
    let photoURLs = try await listPhotoURLs(inGallery: "Amsterdam Holiday")
    for photoURL in photoURLs {
    taskGroup.addTask { try await downloadPhoto(url: photoURL) }
    }

    var images = [UIImage]()

    /// Note the use of `next()`:
    while let downloadImage = try await taskGroup.next() {
    images.append(downloadImage)
    }
    return images
    }