13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

104CHAPTER 4: <strong>Objective</strong>-C Language FeaturesBy default, all dispatch queues target the default priority global queue. You can change a targetqueue using the dispatch_set_target_queue method.There are several reasons you may want to change the target queue of a custom queue. Youmay want blocks submitted to your custom queue to be executed on the higher priority globalqueue. Or maybe you’d like them executed on main queue. You can even set custom queues astargets of other custom queues, creating a hierarchy of queue targets.NoteDefining a target queue hierarchy with a loop will lead to undefined behavior.If you suspend a queue, any queues that target that queue will also be suspended. This is aneasy way to suspend and resume a large number of queues at once.NoteDo not try to set the target queue of the main queue or a global queue.The final feature of Grand Central Dispatch is the idea of one-time invocation of a block. Recallthe singleton pattern implementation in <strong>Objective</strong>-C (Listing 4-43).Listing 4-43. Singleton in <strong>Objective</strong>-Cstatic MySingleton *sharedInstance = nil;+(MySingleton *)sharedInstance{@syncronized(self){if (!sharedInstance){sharedInstance = [[self alloc] init]; //or other initialization}}}return sharedInstance;The lock used to synchronize the initialization is only really needed once, but it must be lockedand unlocked during every invocation of sharedInstance. This is relatively expensive and canbe replaced by a block that is guaranteed to only ever be executed once. Listing 4-44 is a muchmore efficient and elegant solution to initializing the shared instance of a singleton.Listing 4-44. A More Elegant Singleton Solutionstatic MySingleton *sharedInstance = nil;+(MySingleton *)sharedInstancewww.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!