Skip to content

Instantly share code, notes, and snippets.

@CognitiveDisson
Last active April 29, 2019 14:41
Show Gist options
  • Select an option

  • Save CognitiveDisson/ac772024dfd3f00c466ed035b4b0fbbc to your computer and use it in GitHub Desktop.

Select an option

Save CognitiveDisson/ac772024dfd3f00c466ed035b4b0fbbc to your computer and use it in GitHub Desktop.

Revisions

  1. CognitiveDisson revised this gist Apr 29, 2019. 1 changed file with 7 additions and 6 deletions.
    13 changes: 7 additions & 6 deletions Array+AsyncConcurrentEnumerated.swift
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ import Foundation

    public extension Array {
    public func asyncConcurrentEnumerated(
    each: (_ object: Element, _ completion: (Bool) -> ()) throws -> ()) -> Error?
    each: (_ object: Element, _ completion: @escaping () -> (), _ stop: () -> ()) throws -> ()) throws
    {
    let dispatchGroup = DispatchGroup()
    let array = NSArray(array: self)
    @@ -15,10 +15,9 @@ public extension Array {
    do {
    try each(
    object,
    { stopValue in
    if stopValue == true {
    stop.pointee = true
    }
    { dispatchGroup.leave() },
    {
    stop.pointee = true
    dispatchGroup.leave()
    }
    )
    @@ -29,6 +28,8 @@ public extension Array {
    }
    }
    dispatchGroup.wait()
    return eachError
    if let error = eachError {
    throw error
    }
    }
    }
  2. CognitiveDisson revised this gist Apr 29, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions Array+AsyncConcurrentEnumerated.swift
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,14 @@
    import Foundation

    public extension Array {
    public func asyncConcurrentEnumerated<T>(
    each: (_ object: T, _ completion: (Bool) -> ()) throws -> ()) -> Error?
    public func asyncConcurrentEnumerated(
    each: (_ object: Element, _ completion: (Bool) -> ()) throws -> ()) -> Error?
    {
    let dispatchGroup = DispatchGroup()
    let array = NSArray(array: self)
    var eachError: Error?
    array.enumerateObjects(options: .concurrent) { obj, key, stop in
    guard let object = obj as? T else {
    guard let object = obj as? Element else {
    return
    }
    dispatchGroup.enter()
  3. CognitiveDisson revised this gist Apr 29, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Array+AsyncConcurrentEnumerated.swift
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    import Foundation

    extension Array {
    func asyncConcurrentEnumerated<T>(
    public extension Array {
    public func asyncConcurrentEnumerated<T>(
    each: (_ object: T, _ completion: (Bool) -> ()) throws -> ()) -> Error?
    {
    let dispatchGroup = DispatchGroup()
  4. CognitiveDisson created this gist Apr 29, 2019.
    34 changes: 34 additions & 0 deletions Array+AsyncConcurrentEnumerated.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    import Foundation

    extension Array {
    func asyncConcurrentEnumerated<T>(
    each: (_ object: T, _ completion: (Bool) -> ()) throws -> ()) -> Error?
    {
    let dispatchGroup = DispatchGroup()
    let array = NSArray(array: self)
    var eachError: Error?
    array.enumerateObjects(options: .concurrent) { obj, key, stop in
    guard let object = obj as? T else {
    return
    }
    dispatchGroup.enter()
    do {
    try each(
    object,
    { stopValue in
    if stopValue == true {
    stop.pointee = true
    }
    dispatchGroup.leave()
    }
    )
    } catch {
    eachError = error
    stop.pointee = true
    dispatchGroup.leave()
    }
    }
    dispatchGroup.wait()
    return eachError
    }
    }