// // GCDUtil.swift // HCQuanfangtong // // Created by Apple on 2021/12/29. // Copyright © 2021 Jyp. All rights reserved. // import Foundation //MARK: 延迟执行 public func DELAY(_ delay: Double, closure: @escaping () -> Void) { DispatchQueue.main.asyncAfter(deadline: .now() + delay) { closure() } } //MARK: 主线程执行方法 public func MAIN(_ closure: @escaping ()->Void){ DispatchQueue.main.async { closure() } } //MARK: 后台线程执行方法 public func BACK(_ closure: @escaping ()->Void){ DispatchQueue.global().async { closure() } } //MARK: 定时器 /// 定时器 /// - Parameters: /// - repeating: .never 为不重复 /// - closure: 执行方法 /// - Returns: <#description#> public func TIMER(_ repeating: DispatchTimeInterval, closure: @escaping ()->Void) -> DispatchSourceTimer{ let timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.global()) timer.schedule(deadline: .now(), repeating: repeating) timer.setEventHandler(handler: closure) timer.resume() return timer } extension DispatchQueue{ func barrier_async(_ closure: @escaping ()->Void){ async(group: nil, qos: .background, flags: .barrier, execute: closure) } /// 创建一个并行后台线程队列 /// - Parameter serialName: 队列名称 /// - Returns: <#description#> static func createConcurrentQueue(_ serialName: String?) -> DispatchQueue{ return DispatchQueue(label: serialName ?? "", qos: DispatchQoS.init(qosClass: .background, relativePriority: 0), attributes: DispatchQueue.Attributes.concurrent) } }