Created
April 23, 2015 09:42
-
-
Save neilco/c12991fd752bc665892c to your computer and use it in GitHub Desktop.
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 | |
public let GCD_MAIN_QUEUE = dispatch_get_main_queue() | |
public let GCD_BACKGROUND_QUEUE = dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0) | |
public let GCD_LOW_PRIORITY_QUEUE = dispatch_get_global_queue(QOS_CLASS_UTILITY, 0) | |
public let GCD_DEFAULT_QUEUE = dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0) | |
public let GCD_HIGH_PRIORITY_QUEUE = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0) | |
public func after(delay: NSTimeInterval, block: () -> ()) | |
{ | |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), block); | |
} | |
public func usingMainQueue(block: () -> ()) | |
{ | |
usingQueue(GCD_MAIN_QUEUE, block); | |
} | |
public func usingBackgroundQueue(block: () -> ()) | |
{ | |
usingQueue(GCD_BACKGROUND_QUEUE, block); | |
} | |
public func usingQueue(queue: dispatch_queue_t, block: () -> ()) | |
{ | |
dispatch_async(queue, block); | |
} |
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
// Old way | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { | |
self.measurements = self.getMeasurementData() | |
dispatch_async(dispatch_get_main_queue(), { | |
self.tableView.reloadData() | |
}) | |
}) | |
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(2.0 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), { | |
if let photo = UIImage(named: "me") { | |
self.uploadImage(photo) | |
} | |
}) | |
// ------------------------------------------------------------ | |
// New way | |
usingBackgroundQueue { | |
self.measurements = self.getMeasurementData() | |
usingMainQueue { | |
self.tableView.reloadData() | |
} | |
} | |
after(2.0) { | |
if let photo = UIImage(named: "me") { | |
self.uploadImage(photo) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment