Concurrency and Parallelism in iOS
iOS offers several of ways to run tasks concurrently. This article is a short summary of them.
Grand Central Dispatch (GCD)
A queue based API that executes closures on workers pool of threads in a FIFO manner. GCD decides which thread is used (and not the developer).
Types of queues
- Serial queue
- Tasks are executed one at a time
- Concurrent queue
- Dequeued in a serial fashion
- Execution happens concurrently
Important
- Serial/Concurrent affects the destination queue to which you are dispatching.
- Sync/Async affects the current thread from which you are dispatching.
NSThread / Thread
Manual thread creation using raw NSThread
/Thread
. This is not recommended for various reasons - responsibility to manage the threads with system conditions is on us with all its risks:
- Improper management may cause memory leak in app
- Deallocation once they have finished executing
- Auto release pool will not manage threads created by us
- Maintaining the order of execution
NSOperation / Operation
Under construction