这是一个在 iOS 上易于使用的 GCD 包装器。它不仅提供了针对全局队列的 dispatch_async 的简单直观的包装器,还包括其他有用的构造和概念,如组、计时器、自定义串行和并发队列。
由于这是 GCD 的包装器,因此所有 GCD 概念和限制都适用于此库。如果您不熟悉 GCD,请参阅以下链接
该项目现在是您工作空间的一部分。您需要在目标部分添加其引用到您的应用程序中。
将 iDispatch/iDispatch 源文件夹复制到您的项目中
如果您想顺序执行块/任务,可以使用 SerialQueue 类。以下代码显示了 SerialQueue 类的用法
let queue = SerialQueue(label: "MySerialQueue")
//Execute task asynchronously on this queue
queue.dispatchAsync {
println("This will be executed asynchronously")
}
//Execute task synchronously on this queue
queue.dispatchSync {
println("This will be executed synchronously")
}
//Execute task after number of seconds
queue.dispatchAfterSeconds(3) {
println("This will be executed after 3 seconds")
}
//Suspend queue
queue.suspend()
//Resume queue
queue.resume()
如果您想并发执行块/任务,可以使用 ConcurrentQueue 类。除了 SerialQueue 的所有方法外,该类还提供了一套丰富的方法,它们涵盖了其他 GCD 概念,例如栅栏和 apply blocks。
关于 GCD 的 Apple 文档
分发栅栏允许您在并发分发队列中创建一个同步点。当遇到一个栅栏时,并发队列将延迟执行栅栏块(或任何进一步的块)直到所有在栅栏之前提交的块执行完成。在这个点上,栅栏块自行执行。完成之后,队列恢复其正常执行行为。
let queue = ConcurrentQueue(label: "MyConcurrentQueue")
//Execute block asynchronously on this queue
queue.dispatchAsync {
println("Async task to be executed")
}
//Executing barrier block asynchronously
queue.dispatchBarrierAsync {
println("This block will be executed after all the task submitted to this queue before this block are finished")
}
//Executing barrier block synchronously
queue.dispatchBarrierSync {
println("This block will be executed after all the task submitted to this queue before this block are finished")
}
如果您想在数组上执行并发操作,可以使用apply blocks。假设每次迭代都是相互独立的。以下代码演示了这一过程:
let queue = ConcurrentQueue(label: "MyConcurrentQueue")
let imageFiles: [String] = getImagesToBeProcessed() //Some method that will return image file paths that needs to be processed.
//This will block the current thread till all the images are processed.
queue.applySync(imageFiles) { (imageFile) -> Void in
//Process this image file here.
}
//This will NOT block the current thread, completion block will be executed after all the images are processed.
queue.applySync(imageFiles, block: { (imageFile) -> Void in
//Process this image file here.
}) {
//Do any post processing here.
}
apply blocks的另一应用是在数组上并行执行映射操作。以下代码演示了这一过程
let queue = ConcurrentQueue(label: "MyConcurrentQueue")
let messages: [String] = //Some messages to map
//This method will block till all the map operations are over.
let mapped = queue.mapSync(messages, block: { (message) -> String in
//Process message here
return processedMessage.
})
//This method will NOT block, mapped block will be executed when all the mapp operations are finished.
queue.mapAsync(messages, block: { (message) -> String in
//Process message here
return processedMessage.
}) { (mapped) -> Void in
//Do something with message
}
您的映射数组和映射后的数组可能具有不同类型,这里为了简化,两者都是String类型。
以下内容来自苹果的GCD分配组文档
分组blocks可以实现对并发执行的聚合同步。您的应用程序可以提交多个blocks,并跟踪它们何时全部完成,即使它们可能在不同的队列上运行。当所有指定的任务完成之前无法进行进度时,此行为非常有用。
您可以使用DispatchGroup类访问GCD的此功能。DispatchGroup可以与任何特定队列绑定或不绑定。如果与特定队列绑定,则提交的任务/blocks将在该队列上执行。
let queue1 = ConcurrentQueue(label: "MyConcurrentQueue")
let queue2 = ConcurrentQueue(label: "OtherConcurrentQueue")
let group = DispatchGroup(queue: queue1)
//Eexecute block on the queue that was passed to initializer.
group.dispatchAync {
//Some work to do here
}
//Execute block on different queue.
group.dispatchAsyncOnQueue(queue2) {
//Some more work to do on different queue
}
//To wait for specified seconds for all the blocks to finish.
group.waitForSeconds(10)
//To wait indefinitly for all the blocks to finish.
group.waitForever()
//Control will reach here only after all the blocks are finished.
//Alternatively give a block that will be executed when all the blocks are finished.
group.notify {
//Executes when all the blocks are finished.
}
MIT许可证(MIT)
版权所有(C)2015 abdullah-chhatra
特此授予,免费,任何有权获得此软件及其相关文档文件(以下简称"软件")的任何人,在软件上无限制地处理的权利,包括但不限于以下权利:使用、复制、修改、合并、发布、分发、再许可、出售软件副本,并允许向提供软件的人做上述行为,前提是遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或实质性部分中。
软件按"原样"提供,不提供任何形式的保证,无论是明示的、暗示的,包括但不仅限于适销性、特定用途适用性和非侵权性。在任何情况下,作者或版权所有者均不对任何索赔、损害或任何其他责任负责,无论是基于合同、侵权或其他法律,即使是由于软件或其使用或其他交易而引起或与之有关的。