GCD Dispatcher: A Small Objective-C Wrapper around GCD

“GCD Dispatcher”:https://github.com/dewind/JSGCDDispatcher is a small Objective-C wrapper around Apple’s “Grand Central Dispatch”:http://en.wikipedia.org/wiki/Grand_Central_Dispatch. GCD was built to improve and support concurrent code execution using “blocks”:http://goo.gl/I5Tgt. GCD Dispatcher wraps some of the commonly used functions in GCD in Objective-C.

h1. How To Use

GCD Dispatcher wraps executing code on concurrent queues, serial queues, and the main queue. There is also support for running background tasks in iOS.

h3. Execute code on concurrent queue

[[JSGCDDispatcher sharedDispatcher] dispatch:^{
  NSLog(@"Block Executed On %s", dispatch_queue_get_label(dispatch_get_current_queue()));
} priority:DISPATCH_QUEUE_PRIORITY_HIGH];

h3. Execute code on serial queue

[[JSGCDDispatcher sharedDispatcher] dispatchOnSerialQueue:^{
  NSLog(@"Block Executed On %s", dispatch_queue_get_label(dispatch_get_current_queue()));
}];

h3. Submit serial queue completion listener

[[JSGCDDispatcher sharedDispatcher] submitSerialQueueCompletionListener:^{
  NSLog(@"Serial Queue Complete!");
}];

h3. Execute code on main queue (Main Run Loop)

[[JSGCDDispatcher sharedDispatcher] dispatchOnMainThread:^{
  NSLog(@"Block Executed On %s", dispatch_queue_get_label(dispatch_get_current_queue()));
}];

h3. Background Tasks

Background Taks are only supported on iOS devices. Sometimes there are tasks that are important and need to be executed to completion — terminating them would be _bad_. An application can request time to execute a task in the background. GCD Dispatcher abstracts this process in order to make background processing easier.

[[JSGCDDispatcher sharedDispatcher] dispatchBackgroundTask:^(UIBackgroundTaskIdentifier identifier) {
  if (identifier == UIBackgroundTaskInvalid) {
    NSLog(@"Background Task is Not Valid!");
  } else {
    [NSThread sleepForTimeInterval:4];
    NSLog(@"Background Task is Complete");
  }
} priority:DISPATCH_QUEUE_PRIORITY_DEFAULT];