This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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) | |
| display(thumbnails, in: cell) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @MainActor | |
| class MyDelegate: UICollectionViewDelegate { | |
| func collectionView(_ view: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt item: IndexPath) { | |
| let ids = getThumbnailIDs(for: item) | |
| Task { | |
| let thumbnails = await fetchThumbnails(for: ids) | |
| display(thumbnails, in: cell) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @MainActor | |
| class MyDelegate: UICollectionViewDelegate { | |
| func collectionView(_ view: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt item: IndexPath) { | |
| let ids = getThumbnailIDs(for: item) | |
| // Error: 'await' in a function that does not support concurrency | |
| let thumbnails = await fetchThumbnails(for: ids) | |
| display(thumbnails, in: cell) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func fetchThumbnails(for ids: [String]) async throws -> [String: UIImage] { | |
| var thumbnails: [String: UIImage] = [:] | |
| try await withThrowingTaskGroup(of: (String, UIImage).self) { group in | |
| for id in ids { | |
| group.async { | |
| return (id, try await fetchOneThumbnail(withID: id)) | |
| } | |
| } | |
| // Obtain results from the child tasks, sequentially, in order of completion. | |
| for try await (id, thumbnail) in group { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func fetchThumbnails(for ids: [String]) async throws -> [String: UIImage] { | |
| var thumbnails: [String: UIImage] = [:] | |
| try await withThrowingTaskGroup(of: Void.self) { group in | |
| for id in ids { | |
| group.async { | |
| // Error: Mutation of captured var 'thumbnails' in concurrently executing code | |
| thumbnails[id] = try await fetchOneThumbnail(withID: id) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func fetchThumbnails(for ids: [String]) async throws -> [String: UIImage] { | |
| var thumbnails: [String: UIImage] = [:] | |
| for id in ids { | |
| thumbnails[id] = try await fetchOneThumbnail(withID: id) | |
| } | |
| return thumbnails | |
| } | |
| func fetchOneThumbnail(withID id: String) async throws -> UIImage { | |
| // ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func fetchThumbnails(for ids: [String]) async throws -> [String: UIImage] { | |
| var thumbnails: [String: UIImage] = [:] | |
| for id in ids { | |
| try Task.checkCancellation() | |
| thumbnails[id] = try await fetchOneThumbnail(withID: id) | |
| } | |
| return thumbnails | |
| } | |
| func fetchThumbnails(for ids: [String]) async throws -> [String: UIImage] { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func fetchOneThumbnail(withID id: String) async throws -> UIImage { | |
| let imageReq = imageRequest(for: id), metadataReq = metadataRequest(for: id) | |
| let (data, _) = try await URLSession.shared.data(for: imageReq) | |
| let (metaData, _) = try await URLSession.shared.data(for: metadataReq) | |
| guard let size = parseSize(from: metadata), | |
| let image = await UIImage(data: data)?.byPreparingThumbnail(ofSize: size) | |
| else { | |
| throw ThumbnailFailedError() | |
| } | |
| return Image |
NewerOlder