Created
June 29, 2015 15:35
-
-
Save atomkirk/3ebe96a86bfb7c159bc3 to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// Q.swift | |
// Created by Adam Kirk on 6/6/15. | |
// | |
import Foundation | |
public typealias QBlock = () -> Void | |
func serialQueueWithName(name: String) -> NSOperationQueue { | |
let queue = NSOperationQueue() | |
queue.name = name | |
queue.maxConcurrentOperationCount = 1 | |
return queue | |
} | |
public class Q: NSObject { | |
private static let filesQueue = serialQueueWithName("com.spoken.files-queue") | |
private static let uploaderQueue = serialQueueWithName("com.spoken.uploader-queue") | |
private static let photosQueue = serialQueueWithName("com.spoken.photos-queue") | |
public class func main(block: QBlock) { | |
NSOperationQueue.mainQueue().addOperationWithBlock(block) | |
} | |
public class func files(block: QBlock) { | |
filesQueue.addOperationWithBlock(block) | |
} | |
public class func uploader(block: QBlock) { | |
uploaderQueue.addOperationWithBlock(block) | |
} | |
public class func photos(block: QBlock) { | |
photosQueue.addOperationWithBlock(block) | |
} | |
// MARK: - standard queues | |
public class func def(block: QBlock) { | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block); | |
} | |
public class func background(block: QBlock) { | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), block); | |
} | |
public class func high(block: QBlock) { | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), block); | |
} | |
public class func low(block: QBlock) { | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), block); | |
} | |
public class func delay(seconds: NSTimeInterval, block: QBlock) { | |
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(seconds * Double(NSEC_PER_SEC))) | |
dispatch_after(delayTime, dispatch_get_main_queue(), block) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment