Created
June 16, 2016 07:00
-
-
Save quangtqag/e41c8f72a8f98711ffbcf34e11ad9c90 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
// | |
// Reference: https://www.raywenderlich.com/79150/grand-central-dispatch-tutorial-swift-part-2 | |
// | |
var GlobalMainQueue: dispatch_queue_t { | |
return dispatch_get_main_queue() | |
} | |
var GlobalUserInteractiveQueue: dispatch_queue_t { | |
return dispatch_get_global_queue(Int(QOS_CLASS_USER_INTERACTIVE.value), 0) | |
} | |
var GlobalUserInitiatedQueue: dispatch_queue_t { | |
return dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.value), 0) | |
} | |
var GlobalUtilityQueue: dispatch_queue_t { | |
return dispatch_get_global_queue(Int(QOS_CLASS_UTILITY.value), 0) | |
} | |
var GlobalBackgroundQueue: dispatch_queue_t { | |
return dispatch_get_global_queue(Int(QOS_CLASS_BACKGROUND.value), 0) | |
} | |
// | |
// Dispatch Group | |
// | |
func downloadPhotosWithCompletion(completion: BatchPhotoDownloadingCompletionClosure?) { | |
var storedError: NSError! | |
var downloadGroup = dispatch_group_create() | |
for address in [OverlyAttachedGirlfriendURLString, SuccessKidURLString, LotsOfFacesURLString] | |
{ | |
let url = NSURL(string: address) | |
dispatch_group_enter(downloadGroup) | |
let photo = DownloadPhoto(url: url!) { | |
image, error in | |
if let error = error { | |
storedError = error | |
} | |
dispatch_group_leave(downloadGroup) | |
} | |
PhotoManager.sharedManager.addPhoto(photo) | |
} | |
dispatch_group_notify(downloadGroup, GlobalMainQueue) { | |
if let completion = completion { | |
completion(error: storedError) | |
} | |
} | |
} | |
// | |
// Dispatch Barrier | |
// | |
private let concurrentPhotoQueue = dispatch_queue_create("com.raywenderlich.GooglyPuff.photoQueue", DISPATCH_QUEUE_CONCURRENT) | |
func addPhoto(photo: Photo) { | |
dispatch_barrier_async(concurrentPhotoQueue) { // 1 | |
self._photos.append(photo) // 2 | |
dispatch_async(GlobalMainQueue) { // 3 | |
self.postContentAddedNotification() | |
} | |
} | |
} | |
var photos: [Photo] { | |
var photosCopy: [Photo]! | |
dispatch_sync(concurrentPhotoQueue) { // 1 | |
photosCopy = self._photos // 2 | |
} | |
return photosCopy | |
} | |
// | |
// Dispatch Source | |
// | |
#if DEBUG | |
private var signalSource: dispatch_source_t! | |
private var signalOnceToken = dispatch_once_t() | |
#endif | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
#if DEBUG | |
dispatch_once(&signalOnceToken) { | |
let queue = dispatch_get_main_queue() | |
self.signalSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, UInt(SIGSTOP), 0, queue) | |
if let source = self.signalSource { | |
dispatch_source_set_event_handler(source) { | |
NSLog("Hi, I am: \(self.description)") | |
} | |
dispatch_resume(source) | |
} | |
} | |
#endif | |
} | |
(lldb) po self.navigationItem.prompt = "WOOT!" | |
(lldb) e let $vc = unsafeBitCast(0x7fd0b3e22bc0, GooglyPuff.PhotoCollectionViewController.self) | |
(lldb) po $vc.navigationItem.prompt = "WOOT!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment