iOS concurrency, the
run loop, and GCD
BN.University
Slide 2
Slide 2 text
Concurrency in iOS
Mostly managed out of your control
Slide 3
Slide 3 text
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
Slide 4
Slide 4 text
The Run Loop
You’re almost never going to touch a run loop yourself
Important to know about conceptually
Slide 5
Slide 5 text
The run loop
Polls event sources and calls handlers
Slide 6
Slide 6 text
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
Slide 7
Slide 7 text
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
Slide 8
Slide 8 text
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
Slide 9
Slide 9 text
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.
Slide 10
Slide 10 text
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.
Slide 11
Slide 11 text
Tools for Concurrency
NSOperation{,Queue}
Grand Central Dispatch
NSThread
POSIX threads
Slide 12
Slide 12 text
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
Slide 13
Slide 13 text
Tools for Concurrency
NSOperation{,Queue}
Grand Central Dispatch
NSThread
POSIX threads
Slide 14
Slide 14 text
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
Slide 15
Slide 15 text
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
Slide 16
Slide 16 text
Tools for Concurrency
NSOperation{,Queue}
Grand Central Dispatch
NSThread
POSIX threads
Slide 17
Slide 17 text
Grand Central Dispatch
Really interesting stuff
Built on top of libdispatch
block based interface
FIFO queues each with their own priority
Synchronization primitives
Slide 18
Slide 18 text
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
Slide 19
Slide 19 text
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
Slide 20
Slide 20 text
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
Slide 21
Slide 21 text
Tools for Concurrency
NSOperation{,Queue}
Grand Central Dispatch
NSThread
POSIX threads
Slide 22
Slide 22 text
NSOperation{,Queue}
This takes a reusable class-based task approach
Built on top of GCD
Slide 23
Slide 23 text
NSOperation{,Queue}
Each NSOperation is a class and can have individual priorities
NSOperationQueue processes with FIFO + priority
Slide 24
Slide 24 text
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
Slide 25
Slide 25 text
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