Swift-GCD

SwiftGCD

Swift-GCD Package GCDGroup, GCDQueue, GCDSemaphore ,GCDTimer

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate SwiftGCD into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'SwiftGCD'
end

Then, run the following command:

pod install

Example

import UIKit
import SwiftGCD

class ViewController: UIViewController {
    
    var queue     : GCDQueue!
    var group     : GCDGroup!
    var timer     : GCDTimer!
    var semaphore : GCDSemaphore!
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        timerTask()
    }
    
    /**
     普通用法
     */
    func normalTask() {
        
        GCDQueue.globalQueue.excute { () -> Void in
            
            // 子线程执行操作
            
            GCDQueue.mainQueue.excute({ () -> Void in
                
                // 主线程更新UI
            })
        }
        
        
        GCDQueue.executeInGlobalQueue { () -> Void in
            
            // 子线程执行操作
            
            GCDQueue.executeInMainQueue({ () -> Void in
                
                // 主线程更新UI
            })
        }
    }
    
    /**
     延时用法
     */
    func delayTask() {
        
        GCDQueue.executeInGlobalQueue({ () -> Void in
            
            // 延时 2s 执行
            
            }, afterDelaySeconds: .seconds(2))
    }
    
    func waitExecute() {
        
        queue = GCDQueue(queueType: .concurrentQueue)
        
        queue.waitExecute { () -> Void in
            
            print("1")
            sleep(1)
        }
        
        queue.waitExecute { () -> Void in
            
            print("2")
            sleep(1)
        }
        
        queue.waitExecute { () -> Void in
            
            print("3")
            sleep(1)
        }
        
        queue.waitExecute { () -> Void in
            
            print("4")
        }
    }
    
    /**
     设置屏障
     */
    func barrierExecute() {
        
        queue = GCDQueue(queueType: .concurrentQueue)
        
        queue.excute { () -> Void in
            
            print("1")
        }
        
        queue.excute { () -> Void in
            
            print("2")
        }
        
        queue.excute { () -> Void in
            
            print("3")
            sleep(1)
        }
        
        queue.barrierExecute { () -> Void in
            
            print("barrierExecute")
        }
        
        queue.excute { () -> Void in
            
            print("4")
        }
        
        queue.excute { () -> Void in
            
            print("5")
        }
        
        queue.excute { () -> Void in
            
            print("6")
        }
    }
    
    /**
     GCDGroup的使用
     */
    func groupTask() {
        
        group = GCDGroup()
        queue = GCDQueue()
        
        queue.excute({ () -> Void in
            
            print("1")
            
            }, inGroup: group)
        
        queue.excute({ () -> Void in
            
            print("2")
            
            }, inGroup: group)
        
        queue.excute({ () -> Void in
            
            print("3")
            
            }, inGroup: group)
        
        queue.excute({ () -> Void in
            
            print("4")
            
            }, inGroup: group)
        
        queue.excute({ () -> Void in
            
            print("5")
            
            }, inGroup: group)
        
        queue.excute({ () -> Void in
            
            print("6")
            
            }, inGroup: group)
        
        queue.excute({ () -> Void in
            
            print("7")
            
            }, inGroup: group)
        
        queue.excute({ () -> Void in
            
            print("8")
            
            }, inGroup: group)
        
        queue.notify({ () -> Void in
            
            print("都完成了")
            
            }, inGroup: group)
    }
    
    /**
     GCD信号量的使用
     */
    func semaphoreTask() {
        
        semaphore = GCDSemaphore()
        queue     = GCDQueue(queueType: .concurrentQueue)
        
        queue.excute { () -> Void in
            
            print("1")
            _  = self.semaphore.signal()
        }
        
        queue.excute { () -> Void in
            
            print("2")
            _ = self.semaphore.signal()
        }
        
        queue.excute { () -> Void in
            
            print("3")
           _ = self.semaphore.signal()
        }
        
        queue.excute { () -> Void in
            
            print("4")
           _ = self.semaphore.signal()
        }
        
        queue.excute { () -> Void in
            
            self.semaphore.wait()
            self.semaphore.wait()
            self.semaphore.wait()
            self.semaphore.wait()
            
            print("都完成了")
        }
    }
    
    /**
     GCDTimer的使用
     */
    func timerTask() {
        
        timer = GCDTimer(inQueue: GCDQueue.globalQueue)
        timer.eventRepeating(interval: .seconds(2)) {
            print("重复任务")
        }
        timer.start()
    }
}

Demo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,507评论 0 23
  • 转载自:https://github.com/Tim9Liu9/TimLiu-iOS[https://github...
    香橙柚子阅读 12,895评论 0 36
  • 本篇文章介绍的是在Swift3语言下的GCD应用操作,针对的目标读者可以是没有GCD基础的初学者,也可以是对GCD...
    撸码是一种情怀阅读 9,663评论 0 12
  • 风啸 ——自题 夜细步而来, 风肆意屋外。 吾扶梯登楼, 月浮沉云海。 沙落寞成灾, 丝飘舞如埃。 灯绰绰影影, ...
    灵空_6039阅读 1,864评论 0 2
  • 我讨厌怨天尤人,因为我觉得这样根本无济于事,可是有时我又会无力于自己力量的渺小以及自己的柔弱。我企图坚强,什么都一...
    Cuttie阅读 1,720评论 0 0

友情链接更多精彩内容