GCDUtil.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //
  2. // GCDUtil.swift
  3. // HCQuanfangtong
  4. //
  5. // Created by Apple on 2021/12/29.
  6. // Copyright © 2021 Jyp. All rights reserved.
  7. //
  8. import Foundation
  9. //MARK: 延迟执行
  10. public func DELAY(_ delay: Double, closure: @escaping () -> Void) {
  11. DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
  12. closure()
  13. }
  14. }
  15. //MARK: 主线程执行方法
  16. public func MAIN(_ closure: @escaping ()->Void){
  17. DispatchQueue.main.async {
  18. closure()
  19. }
  20. }
  21. //MARK: 后台线程执行方法
  22. public func BACK(_ closure: @escaping ()->Void){
  23. DispatchQueue.global().async {
  24. closure()
  25. }
  26. }
  27. //MARK: 定时器
  28. /// 定时器
  29. /// - Parameters:
  30. /// - repeating: .never 为不重复
  31. /// - closure: 执行方法
  32. /// - Returns: <#description#>
  33. public func TIMER(_ repeating: DispatchTimeInterval, closure: @escaping ()->Void) -> DispatchSourceTimer{
  34. let timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.global())
  35. timer.schedule(deadline: .now(), repeating: repeating)
  36. timer.setEventHandler(handler: closure)
  37. timer.resume()
  38. return timer
  39. }
  40. extension DispatchQueue{
  41. func barrier_async(_ closure: @escaping ()->Void){
  42. async(group: nil, qos: .background, flags: .barrier, execute: closure)
  43. }
  44. /// 创建一个并行后台线程队列
  45. /// - Parameter serialName: 队列名称
  46. /// - Returns: <#description#>
  47. static func createConcurrentQueue(_ serialName: String?) -> DispatchQueue{
  48. return DispatchQueue(label: serialName ?? "", qos: DispatchQoS.init(qosClass: .background, relativePriority: 0), attributes: DispatchQueue.Attributes.concurrent)
  49. }
  50. }