SlideShare a Scribd company logo
Study Group
GCD and
NSOperationQueue
Joe Hsieh
2015/04/14
Outline
• Introduction of GCD 

(Use GCD to Take Advantage of Platform Scaling)
• Introduction of NSOperationQueue and
NSOperation
• Relation between GCD and NSOperationQueue

(When to Use GCD and When to Use Operation ?)
• Example
Grand Central Dispatch
(GCD)
• GCD is a concurrency library implemented via
FIFO queues.(In order to solve the problems
caused by multi-threading)
• GCD is just the marketing name for libdispatch.
Queues of GCD
• Serial Queue:The order of execution and completion is FIFO.
• dispatch_get_main_queue
• dispatch_queue_create(“com.ninja.serial”, DISPATCH_QUEUE_SERIAL) // or
NULL
• Concurrent Queue:The order of execution is FIFO, but the order of completion is
not necessarily FIFO.
• dispatch_get_global_queue:There are many global queues 

Before iOS 8 :high、default、low

After iOS 8:QOS_CLASS(_USER_INTERACTIVE、_INITIATED、_UTILITY、
_BACKGROUND) dispatch_get_global_queue:There are many global queues
• dispatch_queue_create(“com.ninja.concurrent”,
DISPATCH_QUEUE_CONCURRENT)
Queues of GCD
3 2 1
3 2 1
Serial Queue
Concurrent Queue
Order of completion

(each task needs 1s)
1, 2, 3。Required 3s
2, 1, 3。Required 1.1s
4
4
p.s tasks in queue are blocks.
…
…
Dispatching API
3 2 1
Serial Queue 

or Concurrent Queue
4
Caller thread:Should I wait for block 4 done?
Option 1

I am waiting for block 4
dispatch_sync
Option 2

fire and forget block 4
dispatch_async
…
Can we know the timing of all
tasks in the queue are done?
• Yes, we can make it by a counter !
• Enqueue block to the queue and then counter
plus 1.When block is finished, counter minus
1.When counter is zero, system will notify us.
Dispatch Groups
• Dispatch groups are a GCD feature that allows
you to easily group tasks. You can then wait on
that set of tasks to finish or be notified through a
callback when the set of tasks has finished.
Dispatch Groups
dispatch_group_t group = dispatch_group_create();
dispatch_group_notify(group, dispatch_get_main_queue(), ^{ //When groupCounter
is zero,system notifies main thread to execute completionBlock
completionBlock();
});
// form1
dispatch_release(group);
dispatch_group_enter(group); // groupCounter++
dispatch_async(group, queue, ^{
block1();
dispatch_group_leave(group); // groupCounter—-
dispatch_release(group);
});
// It is the same as form1
dispatch_group_async(group, queue, ^{
block2();
});
Relation between GCD and
Operation Queues
• Under the hood, NSOperationQueue(Objective-
C) is based on GCD (C) from iOS 4 and Mac OS
X 10.6.
• NSOperationQueue and NSOperation provide
more high-level API by low-level API of GCD.
NSOperationQueue
3 2 1
NSOperationQueue
(concurrent)
4
add operation 4
into operationQueue

waitUntilFinished?
maxConcurrentOperationCount > 3
…
The concurrent (or asynchronize) property of
operation must be NO
when using NSOperation with NSOperationQueue
p.s tasks in queue are operations.
High-level API
• Canceling operations
• Operation priorities
• Operation dependencies
• Because NSOperation is an Objective-C object
• KVO of operation properties
• Reuse of Operations
Example
Simple Montage Image
By AFNetworking
How to do it ?
• Fire four operation to fetch image
• Create four operation into operationQueue.
• Wait for all image fetched.
• Wait for the group of image operations to complete.
• Execute function to create montage image
• System notifies main thread to create montage
image.
Reference
• http://commandshift.co.uk/blog/2014/03/19/using-
dispatch-groups-to-wait-for-multiple-web-services/
• https://developer.apple.com/library/prerelease/ios/
documentation/Cocoa/Reference/
NSOperation_class/index.htm/
• http://nshipster.com/nsoperation/
• https://gist.github.com/joehsieh/
747366e8de86a8b80983
Q&A

More Related Content

PDF
Quick sort
PPTX
C functions by ranjan call by value and reference.pptx
PPTX
Basic python programs
PPTX
Combinational Logic Circuit
PDF
Effective Modern C++ - Item 35 & 36
PPTX
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
PPTX
Programing techniques
PPTX
Stack - Data Structure
Quick sort
C functions by ranjan call by value and reference.pptx
Basic python programs
Combinational Logic Circuit
Effective Modern C++ - Item 35 & 36
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Programing techniques
Stack - Data Structure

Similar to GCD and OperationQueue. (20)

PDF
Tech Talk #4 : Multi - threading and GCD ( grand central dispatch ) in iOS - ...
PDF
Grand Central Dispatch - iOS Conf SG 2015
PPTX
Grand Central Dispatch
PDF
You are to simulate a dispatcher using a priority queue system.New.pdf
PDF
Gcd cc-150205
KEY
Blocks & Grand Central Dispatch
PDF
Gcd and concurrency programming
PPTX
Grand Central Dispatch in Objective-C
PDF
Grand Central Dispatch and multi-threading [iCONdev 2014]
PDF
MFF UK - Advanced iOS Topics
PDF
Threads, Queues, and More: Async Programming in iOS
PDF
KEY
Grand Central Dispatch
PDF
Multithreading and Parallelism on iOS [MobOS 2013]
PDF
Multithreading on iOS
PPTX
Pune-Cocoa: Blocks and GCD
PDF
Gcd(i os & os x 멀티스레딩 기법)
PDF
[iOS] Multiple Background Threads
KEY
Grand Central Dispatch Design Patterns
PDF
iOS 2 - The practical Stuff
Tech Talk #4 : Multi - threading and GCD ( grand central dispatch ) in iOS - ...
Grand Central Dispatch - iOS Conf SG 2015
Grand Central Dispatch
You are to simulate a dispatcher using a priority queue system.New.pdf
Gcd cc-150205
Blocks & Grand Central Dispatch
Gcd and concurrency programming
Grand Central Dispatch in Objective-C
Grand Central Dispatch and multi-threading [iCONdev 2014]
MFF UK - Advanced iOS Topics
Threads, Queues, and More: Async Programming in iOS
Grand Central Dispatch
Multithreading and Parallelism on iOS [MobOS 2013]
Multithreading on iOS
Pune-Cocoa: Blocks and GCD
Gcd(i os & os x 멀티스레딩 기법)
[iOS] Multiple Background Threads
Grand Central Dispatch Design Patterns
iOS 2 - The practical Stuff
Ad

Recently uploaded (20)

PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
ai tools demonstartion for schools and inter college
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
AI in Product Development-omnex systems
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Understanding Forklifts - TECH EHS Solution
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Transform Your Business with a Software ERP System
PPTX
Online Work Permit System for Fast Permit Processing
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
L1 - Introduction to python Backend.pptx
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
System and Network Administration Chapter 2
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Adobe Illustrator 28.6 Crack My Vision of Vector Design
How Creative Agencies Leverage Project Management Software.pdf
ai tools demonstartion for schools and inter college
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
AI in Product Development-omnex systems
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Understanding Forklifts - TECH EHS Solution
PTS Company Brochure 2025 (1).pdf.......
Transform Your Business with a Software ERP System
Online Work Permit System for Fast Permit Processing
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Odoo POS Development Services by CandidRoot Solutions
Internet Downloader Manager (IDM) Crack 6.42 Build 41
How to Migrate SBCGlobal Email to Yahoo Easily
L1 - Introduction to python Backend.pptx
Which alternative to Crystal Reports is best for small or large businesses.pdf
CHAPTER 2 - PM Management and IT Context
System and Network Administration Chapter 2
Ad

GCD and OperationQueue.

  • 2. Outline • Introduction of GCD 
 (Use GCD to Take Advantage of Platform Scaling) • Introduction of NSOperationQueue and NSOperation • Relation between GCD and NSOperationQueue
 (When to Use GCD and When to Use Operation ?) • Example
  • 3. Grand Central Dispatch (GCD) • GCD is a concurrency library implemented via FIFO queues.(In order to solve the problems caused by multi-threading) • GCD is just the marketing name for libdispatch.
  • 4. Queues of GCD • Serial Queue:The order of execution and completion is FIFO. • dispatch_get_main_queue • dispatch_queue_create(“com.ninja.serial”, DISPATCH_QUEUE_SERIAL) // or NULL • Concurrent Queue:The order of execution is FIFO, but the order of completion is not necessarily FIFO. • dispatch_get_global_queue:There are many global queues 
 Before iOS 8 :high、default、low
 After iOS 8:QOS_CLASS(_USER_INTERACTIVE、_INITIATED、_UTILITY、 _BACKGROUND) dispatch_get_global_queue:There are many global queues • dispatch_queue_create(“com.ninja.concurrent”, DISPATCH_QUEUE_CONCURRENT)
  • 5. Queues of GCD 3 2 1 3 2 1 Serial Queue Concurrent Queue Order of completion
 (each task needs 1s) 1, 2, 3。Required 3s 2, 1, 3。Required 1.1s 4 4 p.s tasks in queue are blocks. … …
  • 6. Dispatching API 3 2 1 Serial Queue 
 or Concurrent Queue 4 Caller thread:Should I wait for block 4 done? Option 1
 I am waiting for block 4 dispatch_sync Option 2
 fire and forget block 4 dispatch_async …
  • 7. Can we know the timing of all tasks in the queue are done? • Yes, we can make it by a counter ! • Enqueue block to the queue and then counter plus 1.When block is finished, counter minus 1.When counter is zero, system will notify us.
  • 8. Dispatch Groups • Dispatch groups are a GCD feature that allows you to easily group tasks. You can then wait on that set of tasks to finish or be notified through a callback when the set of tasks has finished.
  • 9. Dispatch Groups dispatch_group_t group = dispatch_group_create(); dispatch_group_notify(group, dispatch_get_main_queue(), ^{ //When groupCounter is zero,system notifies main thread to execute completionBlock completionBlock(); }); // form1 dispatch_release(group); dispatch_group_enter(group); // groupCounter++ dispatch_async(group, queue, ^{ block1(); dispatch_group_leave(group); // groupCounter—- dispatch_release(group); }); // It is the same as form1 dispatch_group_async(group, queue, ^{ block2(); });
  • 10. Relation between GCD and Operation Queues • Under the hood, NSOperationQueue(Objective- C) is based on GCD (C) from iOS 4 and Mac OS X 10.6. • NSOperationQueue and NSOperation provide more high-level API by low-level API of GCD.
  • 11. NSOperationQueue 3 2 1 NSOperationQueue (concurrent) 4 add operation 4 into operationQueue
 waitUntilFinished? maxConcurrentOperationCount > 3 … The concurrent (or asynchronize) property of operation must be NO when using NSOperation with NSOperationQueue p.s tasks in queue are operations.
  • 12. High-level API • Canceling operations • Operation priorities • Operation dependencies • Because NSOperation is an Objective-C object • KVO of operation properties • Reuse of Operations
  • 14. How to do it ? • Fire four operation to fetch image • Create four operation into operationQueue. • Wait for all image fetched. • Wait for the group of image operations to complete. • Execute function to create montage image • System notifies main thread to create montage image.
  • 16. Q&A