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.

70CHAPTER 3: Foundational APIspreviously, the GCD API is very minimal. There is very little boilerplate code required to makesome of your code run asynchronously. While this is not true for NSOperation (you actually haveto subclass objects and more) you do have more control; the NSOperation API provides theability to cancel individual operations and to provide some priority ordering between them, whilea GCD queue is a strict FIFO queue. You can also explicitly wait (blocking the current thread)for a given NSOperation to complete, or for all operations on a queue to complete, while GCDprovides no means of determining even whether a given queue has any blocks enqueued at agiven time.We will cover NSOperation and GCD in more detail in a later chapter, since there’s a lot ofinformation to cover there.Run LoopsRun loops, or NSRunLoop objects, are used to implement asynchronous behavior. At their core,run loops use Mach microkernel messages. These messages are designed for inter-processcommunication and are a very fast, safe, and efficient way to have marshal messages from onethread to another.The basics of this marshaling involve accepting messages posted from sources and deliveringthem to their destinations. The sources can be any event, such as a timer, a user interactionevent, or a network event. Run loops are the destinations.The Foundation class, NSRunLoop, is actually a wrapper for a lower-level API class calledCFRunLoop (the CF stands for “Core Foundation”). Run loops operate based on run loop modes,which are string constants assigned to them. The mode of a run loop defines which sources therun loop can handle and, by extension, which messages it marshals.The most famous run loop, the one you’ve implicitly used already, is the main run loop; it isused to update user interface elements on the screen. The main run loop is used to updateelements on the screen, but is not used to handle interaction events. Another run loop, whosemode is UITrackingRunLoopMode on iOS and on NSEventHandlingRunLoopMode OS X, accepts userinteraction events like tapping or clicking a button.Implementing a synchronous, long-running task on the main thread (which will stall the mainrun loop) will stop all user interface updates (animations and other UI updates will stop until theoperation has completed). Stalling the UI tracking run loop, which you can do by holding youfinger down while scrolling a web view, stops other UI tracking. Divorcing these two operations(interface updates and interface interaction tracking) allows content in the web view to beupdated as another run loop handles the event tracking.There is another run loop that you may become familiar with as you begin to load resources fromthe network. All network requests are processed in their own run loop—even the synchronousones! The difference between synchronous and asynchronous network requests is that thesynchronous ones wrap equivalent asynchronous requests and stall whatever run loop they arecalled from until the request completes.Run loops, by default, do not automatically have autorelease pools. While ARC frees <strong>Objective</strong>-Cdevelopers from many hassles associated with memory management, this is one area youstill need to be very much aware of if you’re using run loops. Autorelease pools are covered inChapter 4.www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!