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
![Page 1: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/1.jpg)
An Introduction To Multithreaded Applications Using Grand Central DispatchDavid Fox - www.davefoxy.com
![Page 2: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/2.jpg)
Multithreading
![Page 3: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/3.jpg)
So What IS GCD?
![Page 4: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/4.jpg)
^{ Prerequisite Knowledge}
![Page 5: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/5.jpg)
BlocksPortable Chunks Of Code
![Page 6: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/6.jpg)
What’s A Block Look Like?
^{// code goes here
}
An in-line block
![Page 7: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/7.jpg)
What’s A Block Look Like?A block stored in a reusable variable
![Page 8: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/8.jpg)
What’s A Block Look Like?A block using a custom block type definition
![Page 9: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/9.jpg)
Back To The Point
![Page 10: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/10.jpg)
The GCD Workflow
Create a queue to dispatch to
Dispatch blocks of code to that queue
And that’s basically it! (sort of)
![Page 11: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/11.jpg)
In Action...An Unresponsive App
![Page 13: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/13.jpg)
Recap...
Create a queue with
Dispatch blocks of code using
Don’t mess with the UI on other threads
![Page 14: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/14.jpg)
Taking It FurtherDispatch Groups
![Page 15: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/15.jpg)
When Has GCD Finished A Block?Using The Regular dispatch_async
Output
![Page 16: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/16.jpg)
When Has GCD Finished A Block?Using A Dispatch Group
Output
![Page 17: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/17.jpg)
Encapsulating QueuesUsing NSOperationQueue
![Page 18: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/18.jpg)
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
![Page 19: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/19.jpg)
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.
![Page 20: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/20.jpg)
Operation Queues In ActionListening For Individual Operation Completion
![Page 21: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/21.jpg)
Operation Queues In ActionWaiting Until An Operation Queue Is Complete Before Continuing
![Page 22: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/22.jpg)
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
![Page 23: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/23.jpg)
Loads More To Explore
Blocks instead of delegatesDispatch Resources
Delayed dispatches
Concurrent Operations
Semaphores
Check The Apple Documentation
![Page 24: Multithreaded Applications Using Grand Central Dispatch](https://reader033.vdocuments.net/reader033/viewer/2022052507/558682b1d8b42a314f8b4651/html5/thumbnails/24.jpg)
Thanks