multithreaded applications using grand central dispatch
DESCRIPTION
20 minute presentation on using Apple's Grand Central Dispatch for multithreading iOS applications. This talk was given at LiDG by David Fox on the 6th of April 2011.TRANSCRIPT
An Introduction To Multithreaded Applications Using Grand Central DispatchDavid Fox - www.davefoxy.com
Multithreading
So What IS GCD?
^{ Prerequisite Knowledge}
BlocksPortable Chunks Of Code
What’s A Block Look Like?
^{// code goes here
}
An in-line block
What’s A Block Look Like?A block stored in a reusable variable
What’s A Block Look Like?A block using a custom block type definition
Back To The Point
The GCD Workflow
Create a queue to dispatch to
Dispatch blocks of code to that queue
And that’s basically it! (sort of)
In Action...An Unresponsive App
Recap...
Create a queue with
Dispatch blocks of code using
Don’t mess with the UI on other threads
Taking It FurtherDispatch Groups
When Has GCD Finished A Block?Using The Regular dispatch_async
Output
When Has GCD Finished A Block?Using A Dispatch Group
Output
Encapsulating QueuesUsing NSOperationQueue
The NSOperationQueue Workflow
Subclasses of NSOperation are createdTypically they override -(void) main
An NSOperationQueue is created and NSOperations are added to it
The queue is set in action and the operations are executed
Queues may be suspended so operations must be aware of this
Operation Queues In ActionSimple Asynchronous Operations
Note: As OperationA and OperationB are running on the same queue, operations won’t necessarily finish in the order they’re added.
Operation Queues In ActionListening For Individual Operation Completion
Operation Queues In ActionWaiting Until An Operation Queue Is Complete Before Continuing
Cancelling An Entire Queue
NSOperationQueues offer a cancelAllOperations method
All the NSOperations will be notified of this
They can choose to perform certain actions based upon their isCancelled property
Individual operations can be cancelled using cancel
Loads More To Explore
Blocks instead of delegatesDispatch Resources
Delayed dispatches
Concurrent Operations
Semaphores
Check The Apple Documentation
Thanks