multithreaded applications using grand central dispatch

24
An Introduction To Multithreaded Applications Using Grand Central Dispatch David Fox - www.davefoxy.com

Upload: david-fox

Post on 21-Jun-2015

3.599 views

Category:

Education


1 download

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

An Introduction To Multithreaded Applications Using Grand Central DispatchDavid Fox - www.davefoxy.com

Page 2: Multithreaded Applications Using Grand Central Dispatch

Multithreading

Page 3: Multithreaded Applications Using Grand Central Dispatch

So What IS GCD?

Page 4: Multithreaded Applications Using Grand Central Dispatch

^{ Prerequisite Knowledge}

Page 5: Multithreaded Applications Using Grand Central Dispatch

BlocksPortable Chunks Of Code

Page 6: Multithreaded Applications Using Grand Central Dispatch

What’s A Block Look Like?

^{// code goes here

}

An in-line block

Page 7: Multithreaded Applications Using Grand Central Dispatch

What’s A Block Look Like?A block stored in a reusable variable

Page 8: Multithreaded Applications Using Grand Central Dispatch

What’s A Block Look Like?A block using a custom block type definition

Page 9: Multithreaded Applications Using Grand Central Dispatch

Back To The Point

Page 10: Multithreaded Applications Using Grand Central Dispatch

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

In Action...An Unresponsive App

Page 12: Multithreaded Applications Using Grand Central Dispatch

Coding Demo VideoView online at www.davefoxy.com

Page 13: Multithreaded Applications Using Grand Central Dispatch

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

Taking It FurtherDispatch Groups

Page 15: Multithreaded Applications Using Grand Central Dispatch

When Has GCD Finished A Block?Using The Regular dispatch_async

Output

Page 16: Multithreaded Applications Using Grand Central Dispatch

When Has GCD Finished A Block?Using A Dispatch Group

Output

Page 17: Multithreaded Applications Using Grand Central Dispatch

Encapsulating QueuesUsing NSOperationQueue

Page 18: Multithreaded Applications Using Grand Central Dispatch

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

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

Operation Queues In ActionListening For Individual Operation Completion

Page 21: Multithreaded Applications Using Grand Central Dispatch

Operation Queues In ActionWaiting Until An Operation Queue Is Complete Before Continuing

Page 22: Multithreaded Applications Using Grand Central Dispatch

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

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

Thanks