SlideShare uma empresa Scribd logo
1 de 23
Baixar para ler offline
Intro to GCD/Async Cocoa
Carl Brown
CocoaCoders 2015-02-05
Grand Central Dispatch
It’s a switching system you use to
get your work done
Let the OS/Library worry about
how
Just learn to use it correctly
Threads are like train tracks
Multiple parallel ways for your
code to execute
If you’ve done much professional
programming, you have some
concept of threads
We’re not supposed to use Threads
Apple's Concurrency
Programming Guide*:
Page 10: "The Move Away
from Threads"
Page 74: "Migrating Away from
Threads"
* http://developer.apple.com/library/ios/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html
The OS manages the thread pool
Threads are expensive
It will make/reclaim threads on
demand
It does this dynamically to help
your programs (as well as the
other programs on the system)
If you create your own threads,
you get in its way
We’re supposed to use Queues Now
Apple's Concurrency
Programming Guide*:
Page 74: "Replacing Threads
with Dispatch Queues"
* http://developer.apple.com/library/ios/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html
Queues are like Trains
A collection of tasks (cars) one
behind the other being driven by
an engine.
(Some queues are parallel, we’re
not worrying about those for this
discussion).
Queues live on Threads
Any given (serial) Queue Lives on
one thread at a time
Queues can be moved between
threads
The OS does this for you
So don’t worry about it
Just don’t assume that you’re still
on the same thread you were
Or that you’ll stay on the one you
are
One Thread (&∴ One Queue) is Magic
The UI/Main Thread is where
the UI processes events
Don’t block the UI Thread
Only update or change the UI on
the UI Thread
One Queue (&∴ One Thread) is Magic
The Main Queue is where the UI
processes events
Don’t block the Main Queue
Only update or change the UI on
the Main Queue
Make sure you’re on the right Queue
All UI calls go in
dispatch_get_main_queue()
Updating the UI from the wrong
queue makes bad things happen
Make sure you’re off the wrong Queue
ONLY UI calls go in
dispatch_get_main_queue()
Doing non-UI things on the main
queue blocks the UI
Use
dispatch_get_global_queue(XX, 0)
Sometimes it’s hard to tell
For example, Core Data Contexts
with
NSMainQueueConcurrencyType
are on the UI Thread because
they’re assumed to be
communicating with the UI
Updates to them have to be on
the main queue
"Rocket Engine" for Responsive Code
NSAssert(![NSThread isMainThread], @"BOOM");
"Rocket Engine" for Responsive Code
NSAssert(![NSThread isMainThread], @"BOOM");
WARNING: Rocket Engines can EXPLODE in testing.
Move a task to the Main/UI Queue
if (![NSThread isMainThread]) {
dispatch_async(
dispatch_get_main_queue(),
^{
[self runMethod:args];
});
return;
}
}
*For Simplicity: I’m not worrying about self-capture retain cycles here.
Move a task OFF the Main/UI Queue
if ([NSThread isMainThread]) {
dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),
^{
[self runMethod:args];
});
return;
}
}
*For Simplicity: I’m not worrying about self-capture retain cycles here.
NSOperation
Been around since the first iPhone OS SDK
Way to encapsulate the pieces of a task in one place
Can be queried, suspended or canceled
Simple selector call or block variants
NSOperations are placed in NSOperationQueues
NSOperationQueue
Long-lived (presumably) queue that can contain numerous operations
Can be serial or concurrent
Can be suspended or canceled
Nice (but verbose) Objective-C syntax
Will stay on the same thread, if serial
[NSOperationQueue mainQueue] is always on the Main Thread
Dispatch Queues
C-style (concise) syntax
quicker to use in-place
much less typing than declaring an NSOperation and adding to Queue
Harder to manage or cancel
Which to use?
No hard-and-fast rules, but...
I tend to use NSOperations for:
things I'm going to do several times
things that have non-trivial complexity
I tend to use dispatch_async() for things:
with less than 10 or so lines of code
done only once in the App
that won't need to change when spec changes
Questions? Discussion?

Mais conteúdo relacionado

Destaque

Blocks and GCD(Grand Central Dispatch)
Blocks and GCD(Grand Central Dispatch)Blocks and GCD(Grand Central Dispatch)
Blocks and GCD(Grand Central Dispatch)Jigar Maheshwari
 
Blocks & GCD
Blocks & GCDBlocks & GCD
Blocks & GCDrsebbe
 
Pune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCDPune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCDPrashant Rane
 
Tech Talk #4 : Multi - threading and GCD ( grand central dispatch ) in iOS - ...
Tech Talk #4 : Multi - threading and GCD ( grand central dispatch ) in iOS - ...Tech Talk #4 : Multi - threading and GCD ( grand central dispatch ) in iOS - ...
Tech Talk #4 : Multi - threading and GCD ( grand central dispatch ) in iOS - ...Nexus FrontierTech
 
GCD of n Numbers
GCD of n NumbersGCD of n Numbers
GCD of n NumbersSaikat Roy
 
Multithreading and Parallelism on iOS [MobOS 2013]
 Multithreading and Parallelism on iOS [MobOS 2013] Multithreading and Parallelism on iOS [MobOS 2013]
Multithreading and Parallelism on iOS [MobOS 2013]Kuba Břečka
 
Gcd and concurrency programming
Gcd and concurrency programmingGcd and concurrency programming
Gcd and concurrency programmingLi Lin
 
iOS Multithreading
iOS MultithreadingiOS Multithreading
iOS MultithreadingRicha Jain
 
Grand Central Dispatch
Grand Central DispatchGrand Central Dispatch
Grand Central Dispatchcqtt191
 
Swift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-CSwift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-CAlexis Gallagher
 
iOS Developer Interview Questions
iOS Developer Interview QuestionsiOS Developer Interview Questions
iOS Developer Interview QuestionsClark Davidson
 

Destaque (15)

Blocks and GCD(Grand Central Dispatch)
Blocks and GCD(Grand Central Dispatch)Blocks and GCD(Grand Central Dispatch)
Blocks and GCD(Grand Central Dispatch)
 
Blocks & GCD
Blocks & GCDBlocks & GCD
Blocks & GCD
 
Introduction to Swift
Introduction to SwiftIntroduction to Swift
Introduction to Swift
 
Objective c
Objective cObjective c
Objective c
 
Pune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCDPune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCD
 
Tech Talk #4 : Multi - threading and GCD ( grand central dispatch ) in iOS - ...
Tech Talk #4 : Multi - threading and GCD ( grand central dispatch ) in iOS - ...Tech Talk #4 : Multi - threading and GCD ( grand central dispatch ) in iOS - ...
Tech Talk #4 : Multi - threading and GCD ( grand central dispatch ) in iOS - ...
 
GCD of n Numbers
GCD of n NumbersGCD of n Numbers
GCD of n Numbers
 
Multithreading and Parallelism on iOS [MobOS 2013]
 Multithreading and Parallelism on iOS [MobOS 2013] Multithreading and Parallelism on iOS [MobOS 2013]
Multithreading and Parallelism on iOS [MobOS 2013]
 
Gcd and concurrency programming
Gcd and concurrency programmingGcd and concurrency programming
Gcd and concurrency programming
 
iOS Multithreading
iOS MultithreadingiOS Multithreading
iOS Multithreading
 
Grand Central Dispatch
Grand Central DispatchGrand Central Dispatch
Grand Central Dispatch
 
Swift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-CSwift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-C
 
iOS Design Patterns
iOS Design PatternsiOS Design Patterns
iOS Design Patterns
 
iOS Developer Interview Questions
iOS Developer Interview QuestionsiOS Developer Interview Questions
iOS Developer Interview Questions
 
Architecting iOS Project
Architecting iOS ProjectArchitecting iOS Project
Architecting iOS Project
 

Semelhante a Gcd cc-150205

Fowa Miami 09 Cloud Computing Workshop
Fowa Miami 09 Cloud Computing WorkshopFowa Miami 09 Cloud Computing Workshop
Fowa Miami 09 Cloud Computing WorkshopMark Masterson
 
Building a Spring Boot Application - Ask the Audience!
Building a Spring Boot Application - Ask the Audience!Building a Spring Boot Application - Ask the Audience!
Building a Spring Boot Application - Ask the Audience!🎤 Hanno Embregts 🎸
 
Angular performance slides
Angular performance slidesAngular performance slides
Angular performance slidesDavid Barreto
 
Intro To Node.js
Intro To Node.jsIntro To Node.js
Intro To Node.jsChris Cowan
 
香港六合彩 » SlideShare
香港六合彩 » SlideShare香港六合彩 » SlideShare
香港六合彩 » SlideShareyayao
 
Building a Spring Boot Application - Ask the Audience! (from JavaLand 2017)
Building a Spring Boot Application - Ask the Audience!  (from JavaLand 2017)Building a Spring Boot Application - Ask the Audience!  (from JavaLand 2017)
Building a Spring Boot Application - Ask the Audience! (from JavaLand 2017)🎤 Hanno Embregts 🎸
 
Java - A broad introduction
Java - A broad introductionJava - A broad introduction
Java - A broad introductionBirol Efe
 
Plug yourself in and your app will never be the same (1 hr edition)
Plug yourself in and your app will never be the same (1 hr edition)Plug yourself in and your app will never be the same (1 hr edition)
Plug yourself in and your app will never be the same (1 hr edition)Mikkel Flindt Heisterberg
 
Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)indiangarg
 
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
Joomla! Day Chicago 2011 Presentation - Steven PignataroJoomla! Day Chicago 2011 Presentation - Steven Pignataro
Joomla! Day Chicago 2011 Presentation - Steven PignataroSteven Pignataro
 
Core java over view basics introduction by quontra solutions
Core java over view basics introduction by quontra solutionsCore java over view basics introduction by quontra solutions
Core java over view basics introduction by quontra solutionsQUONTRASOLUTIONS
 
High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2Niti Chotkaew
 
High Performance Ajax Applications
High Performance Ajax ApplicationsHigh Performance Ajax Applications
High Performance Ajax ApplicationsJulien Lecomte
 
Setting Apple's UI Automation Free with Appium
Setting Apple's UI Automation Free with AppiumSetting Apple's UI Automation Free with Appium
Setting Apple's UI Automation Free with Appiummobiletestsummit
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to javaciklum_ods
 
Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipseanshunjain
 

Semelhante a Gcd cc-150205 (20)

Fowa Miami 09 Cloud Computing Workshop
Fowa Miami 09 Cloud Computing WorkshopFowa Miami 09 Cloud Computing Workshop
Fowa Miami 09 Cloud Computing Workshop
 
Building a Spring Boot Application - Ask the Audience!
Building a Spring Boot Application - Ask the Audience!Building a Spring Boot Application - Ask the Audience!
Building a Spring Boot Application - Ask the Audience!
 
Angular performance slides
Angular performance slidesAngular performance slides
Angular performance slides
 
Intro To Node.js
Intro To Node.jsIntro To Node.js
Intro To Node.js
 
Best practices android_2010
Best practices android_2010Best practices android_2010
Best practices android_2010
 
香港六合彩 » SlideShare
香港六合彩 » SlideShare香港六合彩 » SlideShare
香港六合彩 » SlideShare
 
Building a Spring Boot Application - Ask the Audience! (from JavaLand 2017)
Building a Spring Boot Application - Ask the Audience!  (from JavaLand 2017)Building a Spring Boot Application - Ask the Audience!  (from JavaLand 2017)
Building a Spring Boot Application - Ask the Audience! (from JavaLand 2017)
 
Java - A broad introduction
Java - A broad introductionJava - A broad introduction
Java - A broad introduction
 
Plug yourself in and your app will never be the same (1 hr edition)
Plug yourself in and your app will never be the same (1 hr edition)Plug yourself in and your app will never be the same (1 hr edition)
Plug yourself in and your app will never be the same (1 hr edition)
 
Operating System Engineering Quiz
Operating System Engineering QuizOperating System Engineering Quiz
Operating System Engineering Quiz
 
Java applet
Java appletJava applet
Java applet
 
Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)
 
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
Joomla! Day Chicago 2011 Presentation - Steven PignataroJoomla! Day Chicago 2011 Presentation - Steven Pignataro
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
 
Core java over view basics introduction by quontra solutions
Core java over view basics introduction by quontra solutionsCore java over view basics introduction by quontra solutions
Core java over view basics introduction by quontra solutions
 
High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2High Performance Ajax Applications 1197671494632682 2
High Performance Ajax Applications 1197671494632682 2
 
High Performance Ajax Applications
High Performance Ajax ApplicationsHigh Performance Ajax Applications
High Performance Ajax Applications
 
Setting Apple's UI Automation Free with Appium
Setting Apple's UI Automation Free with AppiumSetting Apple's UI Automation Free with Appium
Setting Apple's UI Automation Free with Appium
 
Applet in java
Applet in javaApplet in java
Applet in java
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 
Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipse
 

Mais de Carl Brown

GDPR, User Data, Privacy, and Your Apps
GDPR, User Data, Privacy, and Your AppsGDPR, User Data, Privacy, and Your Apps
GDPR, User Data, Privacy, and Your AppsCarl Brown
 
New in iOS 11.3b4 and Xcode 9.3b4
New in iOS 11.3b4 and Xcode 9.3b4New in iOS 11.3b4 and Xcode 9.3b4
New in iOS 11.3b4 and Xcode 9.3b4Carl Brown
 
Managing Memory in Swift (Yes, that's a thing)
Managing Memory in Swift (Yes, that's a thing)Managing Memory in Swift (Yes, that's a thing)
Managing Memory in Swift (Yes, that's a thing)Carl Brown
 
Better Swift from the Foundation up #tryswiftnyc17 09-06
Better Swift from the Foundation up #tryswiftnyc17 09-06Better Swift from the Foundation up #tryswiftnyc17 09-06
Better Swift from the Foundation up #tryswiftnyc17 09-06Carl Brown
 
Generics, the Swift ABI and you
Generics, the Swift ABI and youGenerics, the Swift ABI and you
Generics, the Swift ABI and youCarl Brown
 
Swift GUI Development without Xcode
Swift GUI Development without XcodeSwift GUI Development without Xcode
Swift GUI Development without XcodeCarl Brown
 
what's new in iOS10 2016-06-23
what's new in iOS10 2016-06-23what's new in iOS10 2016-06-23
what's new in iOS10 2016-06-23Carl Brown
 
Open Source Swift: Up and Running
Open Source Swift: Up and RunningOpen Source Swift: Up and Running
Open Source Swift: Up and RunningCarl Brown
 
Parse migration CocoaCoders April 28th, 2016
Parse migration CocoaCoders April 28th, 2016Parse migration CocoaCoders April 28th, 2016
Parse migration CocoaCoders April 28th, 2016Carl Brown
 
Swift 2.2 Design Patterns CocoaConf Austin 2016
Swift 2.2 Design Patterns CocoaConf Austin 2016Swift 2.2 Design Patterns CocoaConf Austin 2016
Swift 2.2 Design Patterns CocoaConf Austin 2016Carl Brown
 
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...Carl Brown
 
Cocoa coders 141113-watch
Cocoa coders 141113-watchCocoa coders 141113-watch
Cocoa coders 141113-watchCarl Brown
 
iOS8 and the new App Store
iOS8 and the new App Store   iOS8 and the new App Store
iOS8 and the new App Store Carl Brown
 
Dark Art of Software Estimation 360iDev2014
Dark Art of Software Estimation 360iDev2014Dark Art of Software Estimation 360iDev2014
Dark Art of Software Estimation 360iDev2014Carl Brown
 
Intro to cloud kit Cocoader.org 24 July 2014
Intro to cloud kit   Cocoader.org 24 July 2014Intro to cloud kit   Cocoader.org 24 July 2014
Intro to cloud kit Cocoader.org 24 July 2014Carl Brown
 
Welcome to Swift (CocoaCoder 6/12/14)
Welcome to Swift (CocoaCoder 6/12/14)Welcome to Swift (CocoaCoder 6/12/14)
Welcome to Swift (CocoaCoder 6/12/14)Carl Brown
 
Writing Apps that Can See: Getting Data from CoreImage to Computer Vision - ...
Writing Apps that Can See: Getting Data from CoreImage to Computer  Vision - ...Writing Apps that Can See: Getting Data from CoreImage to Computer  Vision - ...
Writing Apps that Can See: Getting Data from CoreImage to Computer Vision - ...Carl Brown
 
Introduction to Git Commands and Concepts
Introduction to Git Commands and ConceptsIntroduction to Git Commands and Concepts
Introduction to Git Commands and ConceptsCarl Brown
 
REST/JSON/CoreData Example Code - A Tour
REST/JSON/CoreData Example Code - A TourREST/JSON/CoreData Example Code - A Tour
REST/JSON/CoreData Example Code - A TourCarl Brown
 
360iDev iOS AntiPatterns
360iDev iOS AntiPatterns360iDev iOS AntiPatterns
360iDev iOS AntiPatternsCarl Brown
 

Mais de Carl Brown (20)

GDPR, User Data, Privacy, and Your Apps
GDPR, User Data, Privacy, and Your AppsGDPR, User Data, Privacy, and Your Apps
GDPR, User Data, Privacy, and Your Apps
 
New in iOS 11.3b4 and Xcode 9.3b4
New in iOS 11.3b4 and Xcode 9.3b4New in iOS 11.3b4 and Xcode 9.3b4
New in iOS 11.3b4 and Xcode 9.3b4
 
Managing Memory in Swift (Yes, that's a thing)
Managing Memory in Swift (Yes, that's a thing)Managing Memory in Swift (Yes, that's a thing)
Managing Memory in Swift (Yes, that's a thing)
 
Better Swift from the Foundation up #tryswiftnyc17 09-06
Better Swift from the Foundation up #tryswiftnyc17 09-06Better Swift from the Foundation up #tryswiftnyc17 09-06
Better Swift from the Foundation up #tryswiftnyc17 09-06
 
Generics, the Swift ABI and you
Generics, the Swift ABI and youGenerics, the Swift ABI and you
Generics, the Swift ABI and you
 
Swift GUI Development without Xcode
Swift GUI Development without XcodeSwift GUI Development without Xcode
Swift GUI Development without Xcode
 
what's new in iOS10 2016-06-23
what's new in iOS10 2016-06-23what's new in iOS10 2016-06-23
what's new in iOS10 2016-06-23
 
Open Source Swift: Up and Running
Open Source Swift: Up and RunningOpen Source Swift: Up and Running
Open Source Swift: Up and Running
 
Parse migration CocoaCoders April 28th, 2016
Parse migration CocoaCoders April 28th, 2016Parse migration CocoaCoders April 28th, 2016
Parse migration CocoaCoders April 28th, 2016
 
Swift 2.2 Design Patterns CocoaConf Austin 2016
Swift 2.2 Design Patterns CocoaConf Austin 2016Swift 2.2 Design Patterns CocoaConf Austin 2016
Swift 2.2 Design Patterns CocoaConf Austin 2016
 
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
 
Cocoa coders 141113-watch
Cocoa coders 141113-watchCocoa coders 141113-watch
Cocoa coders 141113-watch
 
iOS8 and the new App Store
iOS8 and the new App Store   iOS8 and the new App Store
iOS8 and the new App Store
 
Dark Art of Software Estimation 360iDev2014
Dark Art of Software Estimation 360iDev2014Dark Art of Software Estimation 360iDev2014
Dark Art of Software Estimation 360iDev2014
 
Intro to cloud kit Cocoader.org 24 July 2014
Intro to cloud kit   Cocoader.org 24 July 2014Intro to cloud kit   Cocoader.org 24 July 2014
Intro to cloud kit Cocoader.org 24 July 2014
 
Welcome to Swift (CocoaCoder 6/12/14)
Welcome to Swift (CocoaCoder 6/12/14)Welcome to Swift (CocoaCoder 6/12/14)
Welcome to Swift (CocoaCoder 6/12/14)
 
Writing Apps that Can See: Getting Data from CoreImage to Computer Vision - ...
Writing Apps that Can See: Getting Data from CoreImage to Computer  Vision - ...Writing Apps that Can See: Getting Data from CoreImage to Computer  Vision - ...
Writing Apps that Can See: Getting Data from CoreImage to Computer Vision - ...
 
Introduction to Git Commands and Concepts
Introduction to Git Commands and ConceptsIntroduction to Git Commands and Concepts
Introduction to Git Commands and Concepts
 
REST/JSON/CoreData Example Code - A Tour
REST/JSON/CoreData Example Code - A TourREST/JSON/CoreData Example Code - A Tour
REST/JSON/CoreData Example Code - A Tour
 
360iDev iOS AntiPatterns
360iDev iOS AntiPatterns360iDev iOS AntiPatterns
360iDev iOS AntiPatterns
 

Gcd cc-150205

  • 1. Intro to GCD/Async Cocoa Carl Brown CocoaCoders 2015-02-05
  • 2. Grand Central Dispatch It’s a switching system you use to get your work done Let the OS/Library worry about how Just learn to use it correctly
  • 3. Threads are like train tracks Multiple parallel ways for your code to execute If you’ve done much professional programming, you have some concept of threads
  • 4. We’re not supposed to use Threads Apple's Concurrency Programming Guide*: Page 10: "The Move Away from Threads" Page 74: "Migrating Away from Threads" * http://developer.apple.com/library/ios/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html
  • 5. The OS manages the thread pool Threads are expensive It will make/reclaim threads on demand It does this dynamically to help your programs (as well as the other programs on the system) If you create your own threads, you get in its way
  • 6. We’re supposed to use Queues Now Apple's Concurrency Programming Guide*: Page 74: "Replacing Threads with Dispatch Queues" * http://developer.apple.com/library/ios/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html
  • 7. Queues are like Trains A collection of tasks (cars) one behind the other being driven by an engine. (Some queues are parallel, we’re not worrying about those for this discussion).
  • 8. Queues live on Threads Any given (serial) Queue Lives on one thread at a time
  • 9. Queues can be moved between threads The OS does this for you So don’t worry about it Just don’t assume that you’re still on the same thread you were Or that you’ll stay on the one you are
  • 10. One Thread (&∴ One Queue) is Magic The UI/Main Thread is where the UI processes events Don’t block the UI Thread Only update or change the UI on the UI Thread
  • 11. One Queue (&∴ One Thread) is Magic The Main Queue is where the UI processes events Don’t block the Main Queue Only update or change the UI on the Main Queue
  • 12. Make sure you’re on the right Queue All UI calls go in dispatch_get_main_queue() Updating the UI from the wrong queue makes bad things happen
  • 13. Make sure you’re off the wrong Queue ONLY UI calls go in dispatch_get_main_queue() Doing non-UI things on the main queue blocks the UI Use dispatch_get_global_queue(XX, 0)
  • 14. Sometimes it’s hard to tell For example, Core Data Contexts with NSMainQueueConcurrencyType are on the UI Thread because they’re assumed to be communicating with the UI Updates to them have to be on the main queue
  • 15. "Rocket Engine" for Responsive Code NSAssert(![NSThread isMainThread], @"BOOM");
  • 16. "Rocket Engine" for Responsive Code NSAssert(![NSThread isMainThread], @"BOOM"); WARNING: Rocket Engines can EXPLODE in testing.
  • 17. Move a task to the Main/UI Queue if (![NSThread isMainThread]) { dispatch_async( dispatch_get_main_queue(), ^{ [self runMethod:args]; }); return; } } *For Simplicity: I’m not worrying about self-capture retain cycles here.
  • 18. Move a task OFF the Main/UI Queue if ([NSThread isMainThread]) { dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ [self runMethod:args]; }); return; } } *For Simplicity: I’m not worrying about self-capture retain cycles here.
  • 19. NSOperation Been around since the first iPhone OS SDK Way to encapsulate the pieces of a task in one place Can be queried, suspended or canceled Simple selector call or block variants NSOperations are placed in NSOperationQueues
  • 20. NSOperationQueue Long-lived (presumably) queue that can contain numerous operations Can be serial or concurrent Can be suspended or canceled Nice (but verbose) Objective-C syntax Will stay on the same thread, if serial [NSOperationQueue mainQueue] is always on the Main Thread
  • 21. Dispatch Queues C-style (concise) syntax quicker to use in-place much less typing than declaring an NSOperation and adding to Queue Harder to manage or cancel
  • 22. Which to use? No hard-and-fast rules, but... I tend to use NSOperations for: things I'm going to do several times things that have non-trivial complexity I tend to use dispatch_async() for things: with less than 10 or so lines of code done only once in the App that won't need to change when spec changes