Created
April 29, 2016 12:56
-
-
Save el-hoshino/20ce24f43665bc6de4655e401404517e to your computer and use it in GitHub Desktop.
dispatch いちいち書くの面倒だったら…自分で改良すればいいじゃない! ref: http://qiita.com/lovee/items/facfe77f6eab16651f08
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let semaphore = dispatch_semaphore_create(0) | |
dispatch_async(dispatch_get_main_queue()) { | |
dispatch_semaphore_signal(semaphore) | |
view.setNeedsDisplay() | |
} | |
dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, Int64(NSEC_PER_SEC))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GCD.runAsynchronizedQueue(waitUntilStartForMax: 1) { | |
view.setNeedsDisplay() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct GCD { | |
enum Thread { | |
case Main | |
case Static(queue: dispatch_queue_t) | |
case Dynamic(name: String, attribute: QueueAttribute) | |
var queue: dispatch_queue_t { | |
switch self { | |
case .Main: | |
return dispatch_get_main_queue() | |
case .Static(queue: let queue): | |
return queue | |
case .Dynamic(name: let name, attribute: let attribute): | |
return dispatch_queue_create(name, attribute.queueAttribute) | |
} | |
} | |
enum QueueAttribute { | |
case Serial | |
case Concurrent | |
var queueAttribute: dispatch_queue_attr_t { | |
switch self { | |
case .Serial: | |
return DISPATCH_QUEUE_SERIAL | |
case .Concurrent: | |
return DISPATCH_QUEUE_CONCURRENT | |
} | |
} | |
} | |
} | |
private init() { | |
} | |
} | |
extension GCD { // MARK: Semaphores | |
enum DispatchTime { | |
case Forever | |
case TimeAfter(delta: NSTimeInterval) | |
var dispatchTimeType: dispatch_time_t { | |
switch self { | |
case .Forever: | |
return DISPATCH_TIME_FOREVER | |
case .TimeAfter(delta: let delta): | |
let nanoseconds = Int64(delta * NSTimeInterval(NSEC_PER_SEC)) | |
let time = dispatch_time(DISPATCH_TIME_NOW, nanoseconds) | |
return time | |
} | |
} | |
} | |
static func createSemaphore(value: Int) -> dispatch_semaphore_t { | |
return dispatch_semaphore_create(value) | |
} | |
static func fireSemaphore(semaphore: dispatch_semaphore_t) { | |
dispatch_semaphore_signal(semaphore) | |
} | |
static func waitForSemaphore(semaphore: dispatch_semaphore_t, until deadLine: DispatchTime = .Forever) { | |
dispatch_semaphore_wait(semaphore, deadLine.dispatchTimeType) | |
} | |
} | |
extension GCD { // MARK: Queues | |
static func runSynchronizedQueue(at thread: Thread = .Main, with action: (() -> Void)) { | |
dispatch_sync(thread.queue) { | |
action() | |
} | |
} | |
static func runAsynchronizedQueue(at thread: Thread = .Main, waitUntilStartForMax waitTime: NSTimeInterval? = nil, with action: (() -> Void)) { | |
if let waitTime = waitTime { | |
let semaphore = GCD.createSemaphore(thread.queue.hash) | |
dispatch_async(thread.queue, { | |
GCD.fireSemaphore(semaphore) | |
action() | |
}) | |
GCD.waitForSemaphore(semaphore, until: waitTime == 0 || waitTime == .infinity ? .Forever : .TimeAfter(delta: waitTime)) | |
} else { | |
dispatch_async(thread.queue) { | |
action() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment