29 lines
677 B
Swift
29 lines
677 B
Swift
|
//
|
||
|
// DispatchGroup+Extensions.swift
|
||
|
// CapCollector
|
||
|
//
|
||
|
// Created by iMac on 13.01.21.
|
||
|
// Copyright © 2021 CH. All rights reserved.
|
||
|
//
|
||
|
|
||
|
import Foundation
|
||
|
|
||
|
extension DispatchGroup {
|
||
|
|
||
|
typealias AsyncSuccessCallback = (Bool) -> Void
|
||
|
|
||
|
static func singleTask(timeout: TimeInterval = 30, _ block: (@escaping AsyncSuccessCallback) -> Void) -> Bool {
|
||
|
let group = DispatchGroup()
|
||
|
group.enter()
|
||
|
var result = true
|
||
|
block { success in
|
||
|
result = success
|
||
|
group.leave()
|
||
|
}
|
||
|
guard group.wait(timeout: .now() + timeout) == .success else {
|
||
|
return false
|
||
|
}
|
||
|
return result
|
||
|
}
|
||
|
}
|