Upgrade to Pro — share decks privately, control downloads, hide ads and more …

iOS Concurrency BN.U w/Notes

iOS Concurrency BN.U w/Notes

A discussion of iOS concurrency and some general iOS application structure. Associated demo at https://github.com/brandnetworks/Image-Loader-Demo, building from master -> develop

Ben Nicholas

August 13, 2014
Tweet

More Decks by Ben Nicholas

Other Decks in Programming

Transcript

  1. main.m Only area where you’re starting things off Everything* else

    is going to be event driven Otherwise, responding to an event, or working through a queue
  2. The Run Loop You’re almost never going to touch a

    run loop yourself Important to know about conceptually
  3. UI Threading There is a very defined “main” thread in

    iOS (and OS X) All UI mutation must happen here Luckily, it’s easy to get back to
  4. Queues Queues are the base concept behind threading in modern

    ObjC They handle the thread they’re running on under the hood Accessed by enqueuing an executable chunk of code or block
  5. Blocks Blocks are closures/lambdas in Objective C Used all over,

    they’re just a block of runnable code Can be called directly or enqueued with libdispatch
  6. Libdispatch This is a really magical library provided by Apple

    It handles how many threads to use for your machine Transparently lets you take advantage of multiple cores/hyperthreading/etc.
  7. Tools for Concurrency This is a really magical library provided

    by Apple It handles how many threads to use for your machine Transparently lets you take advantage of multiple cores/hyperthreading/etc.
  8. POSIX threads Everything is built on top of these Can

    run objc and thread-communication, requires a run loop manually Mostly for performance critical long running tasks
  9. NSThread Abstraction layer on top of posix threads Easy to

    spin off a background thread for a particular selector Easy to bring in a run loop if communication is needed
  10. NSThread Server Connections? File System Watching? Database Connection? Regular (cron-like)

    event? Really only useful for true long running tasks or background general purpose threads (the system will make those background threads) All are event based in Cocoa, last use a timer
  11. Grand Central Dispatch Really interesting stuff Built on top of

    libdispatch block based interface FIFO queues each with their own priority Synchronization primitives
  12. Grand Central Dispatch Parallel Array Iteration dispatch_apply can replace for

    loops almost without change runs in parallel as long as the queue is parallel
  13. Grand Central Dispatch Dispatching a background task That code will

    run on a background queue of low priority This is a very common pattern for long running tasks in response to UI interaction
  14. Grand Central Dispatch Calling back for UI Updates This lets

    you run some long running task, such as loading an image, processing it (edge detection maybe?) and displaying it Keeps the main queue open for other UI updates
  15. NSOperation{,Queue} Each NSOperation is a class and can have individual

    priorities NSOperationQueue processes with FIFO + priority
  16. NSOperation{,Queue} Image Metrics Colorize Resize UI JPEG Upload NSOperationQueue also

    handles dependencies You build a web of dependent operations Just adding operations you depend on to a property NSOperationQueue figures out how to run through it as fast as possible
  17. Demo! We’re going to build NSOperations to - download an

    image - resize it - then put it into an image view. In order to get back to the main queue to work with the UI, we’re going to use GCD