
Multi Threading in iOS Application
Grand Central Dispatch is the term when comes to iOS/Mac Multi-threading is in discussion, its an API available for developer in iOS/Mac. It helps to improving application performance. when performing multiple operations at the same time that time most of the cases application will hang or freeze. Where GCD helps to rectify that.
Before starting how it works, let go through the basics…
What is Process and threads ?
A thread is a path of a execution with in a process. A process will be consisting of multiple threads.
A process will be divided in to multiple threads and this approach will achieve parallelism, then threads can be termed as lightweight process.
What’s Multi threading?

A CPU can perform only one operation at a time refers to the clock cycle. Multi- processing is the technique that CPU switched different process in different time, makes sure that considering all threads required with helps of schedulers.
The CPU switches task, in certain times and treat the next task, some cases we can also put some priorities to task that need to be executed in order
ex: All UI threads will be executed in the main queue, API calls will be implemented in the background queue.
//main queue
DispatchQueue.main.async {
....................
//Do Some thing here
....................
}//priorities that can be specify for queue
.userInteractive
.userInitiated
.default
.utility
.background
.unspecified //background queue
DispatchQueue.global(qos: .background).async {
....................
//Do Some thing here
....................
}
Dispatch queues are FIFO queues to which your application can submit tasks in the form of block objects. Dispatch queues execute tasks either serially or concurrently. Work submitted to dispatch queues executes on a pool of threads managed by the system. Except for the dispatch queue representing your app’s main thread, the system makes no guarantees about which thread it uses to execute a task. know more (documentation by apple)
performing multiple tasks at the same time but no guarantee for the completion of request as in order that are launched. its termed as Concurrent executions.when single task at a time is performed then it is Serial execution.
Sync -When a task is synchronous, the task waits until it completes before the return is executed. In the case of Async it returns immediately and it trigger the completion when the execution finishes.
let background = DispatchQueue.global()
background.async { //async tasks here }
background.sync { //sync tasks here }How we switch background queue to main queue ?
DispatchQueue.global(qos: .background).async {
//execute on background queue
DispatchQueue.main.async {
// updates on main queue
}
}how we can perform group of task with DispatchGroup ?
let queueGroup = DispatchGroup()
queueGroup.enter()
loadFirstOne { queueGroup.leave() }
queueGroup.enter()
loadFirstSecond { queueGroup.leave() }
queueGroup.notify(queue: .main) {
print("All tasks Completed")
}Conclusion
GCD really makes the multi tasking possible :)
Thanks!!
