swift版的GCD封装
生活随笔
收集整理的這篇文章主要介紹了
swift版的GCD封装
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
swift版的GCD封裝
?
說明
本人針對swift封裝了GCD,包括GCDQueue,GCDGroup,GCDTimer以及GCDSemaphore,使用較為便利.
?
源碼
https://github.com/YouXianMing/Swift-GCD
// // GCDQueue.swift // GCD // // http://home.cnblogs.com/u/YouXianMing/ // https://github.com/YouXianMing // // Created by YouXianMing on 15/10/9. // import UIKitenum QueueType {case SerialQueue, // 串行線程隊列ConcurrentQueue, // 并發線程隊列None // 無類型 }class GCDQueue: NSObject {// MARK: 變量var dispatchQueue : dispatch_queue_t!// MARK: 初始化override init() {super.init()dispatchQueue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT)}init(queueType : QueueType) {super.init()switch queueType {case .SerialQueue:dispatchQueue = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL)breakcase .ConcurrentQueue:dispatchQueue = dispatch_queue_create(nil, DISPATCH_QUEUE_CONCURRENT)breakcase .None:dispatchQueue = nilbreak}}// MARK: 單例static let mainQueue : GCDQueue = {let instance = GCDQueue(queueType: .None)instance.dispatchQueue = dispatch_get_main_queue()return instance}()static let globalQueue : GCDQueue = {let instance = GCDQueue(queueType: .None)instance.dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)return instance}()static let highPriorityGlobalQueue : GCDQueue = {let instance = GCDQueue(queueType: .None)instance.dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)return instance}()static let lowPriorityGlobalQueue : GCDQueue = {let instance = GCDQueue(queueType: .None)instance.dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)return instance}()static let backgroundPriorityGlobalQueue : GCDQueue = {let instance = GCDQueue(queueType: .None)instance.dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)return instance}()// MARK: 執行/**Submits a block for asynchronous execution on a dispatch queue and returns immediately.- parameter block: dispatch block*/func excute(block : dispatch_block_t) {dispatch_async(dispatchQueue, block)}func excute(block : dispatch_block_t, afterDelayWithNanoseconds : Int64) {dispatch_after(dispatch_time(DISPATCH_TIME_NOW, afterDelayWithNanoseconds), dispatchQueue, block)}/**Submits a block object for execution on a dispatch queue and waits until that block completes.- parameter block: dispatch block*/func waitExecute(block : dispatch_block_t) {dispatch_sync(dispatchQueue, block)}/**Submits a barrier block for asynchronous execution and returns immediately.- parameter block: dispatch block*/func barrierExecute(block : dispatch_block_t) {dispatch_barrier_async(dispatchQueue, block)}/**Submits a barrier block object for execution and waits until that block completes.- parameter block: dispatch block*/func waitBarrierExecute(block : dispatch_block_t) {dispatch_barrier_sync(dispatchQueue, block)}// MARK: 便利構造器方法class func executeInMainQueue(block : dispatch_block_t) {dispatch_async(mainQueue.dispatchQueue, block)}class func executeInGlobalQueue(block : dispatch_block_t) {dispatch_async(globalQueue.dispatchQueue, block)}class func executeInHighPriorityGlobalQueue(block : dispatch_block_t) {dispatch_async(highPriorityGlobalQueue.dispatchQueue, block)}class func executeInLowPriorityGlobalQueue(block : dispatch_block_t) {dispatch_async(lowPriorityGlobalQueue.dispatchQueue, block)}class func executeInBackgroundPriorityGlobalQueue(block : dispatch_block_t) {dispatch_async(backgroundPriorityGlobalQueue.dispatchQueue, block)}class func executeInMainQueue(block : dispatch_block_t, afterDelaySeconds : Double) {let delayTime : Int64 = Int64(afterDelaySeconds * Double(NSEC_PER_SEC))dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delayTime), mainQueue.dispatchQueue, block)}class func executeInGlobalQueue(block : dispatch_block_t, afterDelaySeconds : Double) {let delayTime : Int64 = Int64(afterDelaySeconds * Double(NSEC_PER_SEC))dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delayTime), globalQueue.dispatchQueue, block)}class func executeInHighPriorityGlobalQueue(block : dispatch_block_t, afterDelaySeconds : Double) {let delayTime : Int64 = Int64(afterDelaySeconds * Double(NSEC_PER_SEC))dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delayTime), highPriorityGlobalQueue.dispatchQueue, block)}class func executeInLowPriorityGlobalQueue(block : dispatch_block_t, afterDelaySeconds : Double) {let delayTime : Int64 = Int64(afterDelaySeconds * Double(NSEC_PER_SEC))dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delayTime), lowPriorityGlobalQueue.dispatchQueue, block)}class func executeInBackgroundPriorityGlobalQueue(block : dispatch_block_t, afterDelaySeconds : Double) {let delayTime : Int64 = Int64(afterDelaySeconds * Double(NSEC_PER_SEC))dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delayTime), backgroundPriorityGlobalQueue.dispatchQueue, block)}// MARK: 恢復與掛起 func suspend() {dispatch_suspend(dispatchQueue)}func resume() {dispatch_resume(dispatchQueue)}// MARK: GCDGroup相關func excute(block : dispatch_block_t, inGroup : GCDGroup!) {dispatch_group_async(inGroup.dispatchGroup, dispatchQueue, block)}func notify(block : dispatch_block_t, inGroup : GCDGroup!) {dispatch_group_notify(inGroup.dispatchGroup, dispatchQueue, block)} } // // GCDGroup.swift // GCD // // http://home.cnblogs.com/u/YouXianMing/ // https://github.com/YouXianMing // // Created by YouXianMing on 15/10/9. // import UIKitclass GCDGroup: NSObject {// MARK: 變量var dispatchGroup : dispatch_group_t!// MARK: 初始化override init() {super.init()dispatchGroup = dispatch_group_create()}// MARK: 操作 func enter() {dispatch_group_enter(dispatchGroup)}func leave() {dispatch_group_leave(dispatchGroup)}func wait() {dispatch_group_wait(dispatchGroup, DISPATCH_TIME_FOREVER)}func waitWithNanoseconds(nanoseconds : Int64) -> Bool {return dispatch_group_wait(dispatchGroup, dispatch_time(DISPATCH_TIME_NOW, nanoseconds)) == 0} } // // GCDTimer.swift // GCD // // http://home.cnblogs.com/u/YouXianMing/ // https://github.com/YouXianMing // // Created by YouXianMing on 15/10/9. // import UIKitclass GCDTimer: NSObject {// MARK: 變量var dispatchSource : dispatch_source_t!// MARK: 初始化override init() {super.init()dispatchSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))}init(inQueue : GCDQueue) {super.init()self.dispatchSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, inQueue.dispatchQueue)}// MARK: 執行func event(block : dispatch_block_t, timeIntervalWithNanoseconds : UInt64) {dispatch_source_set_timer(dispatchSource, dispatch_time(DISPATCH_TIME_NOW, 0), timeIntervalWithNanoseconds, 0)dispatch_source_set_event_handler(dispatchSource) { () -> Void inblock()}}func event(block : dispatch_block_t, timeIntervalWithSeconds : Double) {let timeInterval : UInt64 = UInt64(timeIntervalWithSeconds * Double(NSEC_PER_SEC))dispatch_source_set_timer(dispatchSource, dispatch_time(DISPATCH_TIME_NOW, 0), timeInterval, 0)dispatch_source_set_event_handler(dispatchSource) { () -> Void inblock()}}func start() {dispatch_resume(dispatchSource)}func destroy() {dispatch_source_cancel(dispatchSource)} } // // GCDSemaphore.swift // GCD // // http://home.cnblogs.com/u/YouXianMing/ // https://github.com/YouXianMing // // Created by YouXianMing on 15/10/9. // import UIKitclass GCDSemaphore: NSObject {// MARK: 變量var dispatchSemaphore : dispatch_semaphore_t!// MARK: 初始化override init() {super.init()dispatchSemaphore = dispatch_semaphore_create(0)}init(withValue : Int) {super.init()dispatchSemaphore = dispatch_semaphore_create(withValue)}// 執行func signal() -> Bool {return dispatch_semaphore_signal(dispatchSemaphore) != 0}func wait() {dispatch_semaphore_wait(dispatchSemaphore, DISPATCH_TIME_FOREVER)}func wait(withNanoseconds : Int64) -> Bool {return dispatch_semaphore_wait(dispatchSemaphore, dispatch_time(DISPATCH_TIME_NOW, withNanoseconds)) == 0} } // // ViewController.swift // GCD // // Created by YouXianMing on 15/10/9. // Copyright ? 2015年 YouXianMing. All rights reserved. // import UIKitclass ViewController: UIViewController {var queue : GCDQueue!var group : GCDGroup!var timer : GCDTimer!var semaphore : GCDSemaphore!override func viewDidLoad() {super.viewDidLoad()timerUse()}// MARK: 各種用法/**普通用法*/func normalUse() {GCDQueue.globalQueue.excute { () -> Void in// 子線程執行操作 GCDQueue.mainQueue.excute({ () -> Void in// 主線程更新UI })}GCDQueue.executeInGlobalQueue { () -> Void in// 子線程執行操作 GCDQueue.executeInMainQueue({ () -> Void in// 主線程更新UI })}}/**延時用法*/func delayUse() {GCDQueue.executeInGlobalQueue({ () -> Void in// 延時 2s 執行 }, afterDelaySeconds: 2)}func waitExecute() {queue = GCDQueue(queueType: .ConcurrentQueue)queue.waitExecute { () -> Void inprint("1")sleep(1)}queue.waitExecute { () -> Void inprint("2")sleep(1)}queue.waitExecute { () -> Void inprint("3")sleep(1)}queue.waitExecute { () -> Void inprint("4")}}/**設置屏障*/func barrierExecute() {queue = GCDQueue(queueType: .ConcurrentQueue)queue.excute { () -> Void inprint("1")}queue.excute { () -> Void inprint("2")}queue.excute { () -> Void inprint("3")sleep(1)}queue.barrierExecute { () -> Void inprint("barrierExecute")}queue.excute { () -> Void inprint("4")}queue.excute { () -> Void inprint("5")}queue.excute { () -> Void inprint("6")}}/**GCDGroup的使用*/func groupUse() {group = GCDGroup()queue = GCDQueue()queue.excute({ () -> Void inprint("1")}, inGroup: group)queue.excute({ () -> Void inprint("2")}, inGroup: group)queue.excute({ () -> Void inprint("3")}, inGroup: group)queue.excute({ () -> Void inprint("4")}, inGroup: group)queue.excute({ () -> Void inprint("5")}, inGroup: group)queue.excute({ () -> Void inprint("6")}, inGroup: group)queue.excute({ () -> Void inprint("7")}, inGroup: group)queue.excute({ () -> Void inprint("8")}, inGroup: group)queue.notify({ () -> Void inprint("都完成了")}, inGroup: group)}/**GCDTimer的使用*/func timerUse() {timer = GCDTimer(inQueue: GCDQueue.globalQueue)timer.event({ () -> Void inprint("timer event")}, timeIntervalWithSeconds: 1)timer.start()}/**GCD信號量的使用*/func semaphoreUse() {semaphore = GCDSemaphore()queue = GCDQueue(queueType: .ConcurrentQueue)queue.excute { () -> Void inprint("1")self.semaphore.signal()}queue.excute { () -> Void inprint("2")self.semaphore.signal()}queue.excute { () -> Void inprint("3")self.semaphore.signal()}queue.excute { () -> Void inprint("4")self.semaphore.signal()}queue.excute { () -> Void inself.semaphore.wait()self.semaphore.wait()self.semaphore.wait()self.semaphore.wait()print("都完成了")}} }?
細節
?
轉載于:https://www.cnblogs.com/YouXianMing/p/4864979.html
總結
以上是生活随笔為你收集整理的swift版的GCD封装的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle在不同的语言环境结果to_d
- 下一篇: seo技巧-2015/10/05