Skip to content

Instantly share code, notes, and snippets.

@gaeng2y
Created March 19, 2025 08:32
Show Gist options
  • Select an option

  • Save gaeng2y/df39e385121c8e2d2c7b11db21934be8 to your computer and use it in GitHub Desktop.

Select an option

Save gaeng2y/df39e385121c8e2d2c7b11db21934be8 to your computer and use it in GitHub Desktop.
@MainActor
class MyDelegate: UICollectionViewDelegate {
var thumbnailTasks: [IndexPath: Task<Void, Never>] = [:]
func collectionView(_ view: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt item: IndexPath) {
let ids = getThumbnailIDs(for: item)
thumbnailTasks[item] = Task {
defer { thumbnailTasks[item] = nil }
let thumbnails = await fetchThumbnails(for: ids)
Task.detached(priority: .background) {
withTaskGroup(of: Void.self) { g in
g.async { writeToLocalCache(thumbnails) }
g.async { log(thumbnails) }
g.async { ... }
}
}
display(thumbnails, in: cell)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment