| 123456789101112131415161718192021222324252627282930313233343536373839 |
- //
- // Collection.swift
- // LoopKit
- //
- // Created by Michael Pangburn on 12/4/18.
- // Copyright © 2018 LoopKit Authors. All rights reserved.
- //
- import Dispatch
- import LoopKit
- extension Collection {
- func asyncMap<NewElement>(
- _ asyncTransform: (
- _ element: Element,
- _ completion: @escaping (NewElement) -> Void
- ) -> Void,
- notifyingOn queue: DispatchQueue = .global(),
- completion: @escaping ([NewElement]) -> Void
- ) {
- let result = Locked(Array<NewElement?>(repeating: nil, count: count))
- let group = DispatchGroup()
- for (resultIndex, element) in enumerated() {
- group.enter()
- asyncTransform(element) { newElement in
- result.value[resultIndex] = newElement
- group.leave()
- }
- }
- group.notify(queue: queue) {
- let transformed = result.value.map { $0! }
- completion(transformed)
- }
- }
- }
|