passing completion handler and timout
This commit is contained in:
parent
d65351cabb
commit
9538b50f84
@ -11,10 +11,11 @@ import Foundation
|
||||
public protocol ClientParameterProtocol {
|
||||
init()
|
||||
static var name: String { get }
|
||||
func clientParameter(with paramModel: ClientParameterModelProtocol?) -> [String: Any]?
|
||||
func clientParameter(with paramModel: ClientParameterModelProtocol, _ timout: CGFloat?, completionHandler:@escaping ([String: Any]?) -> ())
|
||||
}
|
||||
|
||||
extension ClientParameterProtocol {
|
||||
|
||||
/// Handle encoding and errors
|
||||
public func getJSON(with paramModel: ClientParameterModelProtocol?) -> JSONDictionary? {
|
||||
guard let dataModel = paramModel,
|
||||
|
||||
@ -37,35 +37,40 @@ import Foundation
|
||||
|
||||
DispatchQueue.global(qos: .userInitiated).async {
|
||||
|
||||
let timeout = clientParameters.optionalCGFloatForKey("timeout") ?? 60.0
|
||||
|
||||
let concurrentQueue = DispatchQueue(label: "com.mva.clientparam", attributes: .concurrent)
|
||||
let group = DispatchGroup()
|
||||
for param in paramsModelList {
|
||||
group.enter()
|
||||
concurrentQueue.async(flags: .barrier) {
|
||||
if let clientParam = clientParameterRegistry.injectParam(param) {
|
||||
paramsList.merge(clientParam) { (_, new) in new }
|
||||
|
||||
clientParameterRegistry.injectParam(param, timeout-1) { (clientParam) in
|
||||
guard let clientParam = clientParam else {
|
||||
group.leave()
|
||||
return
|
||||
}
|
||||
concurrentQueue.async(flags: .barrier) {
|
||||
paramsList.merge(clientParam) { (_, new) in new }
|
||||
group.leave()
|
||||
}
|
||||
group.leave()
|
||||
}
|
||||
}
|
||||
|
||||
if let timeout = clientParameters.optionalCGFloatForKey("timeout") {
|
||||
let dispatchAfterTime = DispatchTimeInterval.seconds(Int(timeout))
|
||||
_ = group.wait(timeout: .now() + dispatchAfterTime)
|
||||
} else {
|
||||
group.wait()
|
||||
}
|
||||
|
||||
let dispatchAfterTime = DispatchTimeInterval.seconds(Int(timeout))
|
||||
_ = group.wait(timeout: .now() + dispatchAfterTime)
|
||||
|
||||
completionHandler(paramsList)
|
||||
}
|
||||
}
|
||||
|
||||
func injectParam( _ param: [String: Any]) -> [String: Any]? {
|
||||
if let paramModel = try? ClientParameterRegistry.getModel(param),
|
||||
let param = ClientParameterRegistry.shared.createParam(paramModel.type) {
|
||||
return param.clientParameter(with: paramModel)
|
||||
func injectParam( _ param: [String: Any], _ timout: CGFloat, completionHandler:@escaping ([String: Any]?) -> ()) {
|
||||
|
||||
guard let paramModel = try? ClientParameterRegistry.getModel(param),
|
||||
let param = ClientParameterRegistry.shared.createParam(paramModel.type) else {
|
||||
return completionHandler(nil)
|
||||
}
|
||||
return nil
|
||||
|
||||
param.clientParameter(with: paramModel, timout, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user