Jul 23, 2022 iOS

Concurrency in iOS swift

Concurrency in iOS swift is the method of running the different task at the same time or in random order. Basically its a multiple task running at same time. Most of the task are run in the different threads. Each task runs at the different thread which runs on the particular interval.

Why Concurrency is used in iOS ?

It is basically critical to understand the app at the user end which the app does not slow down or reduce the performance of the app. Basically the impact is on the scrolling of images or loading the data at the user end. If the user experience the slowness of the app he may uninstall the app. To increase the performance of the app and concurrency comes into picture.

Concurrency is segregated into 2 parts

  1. GCD (Grand Central Dispatch)
  2. Operational Queues

How Concurrency is Achieved ?

  • Time Slicing and Context Switching
  • Interruptions
  • Scheduling Algorithms
  • Latency
  • Time Quantum

Thread :

Thread is basically the running the task at the different core. it is basically running or execution of code .

There are basically 2 types of thread

  1. Main Thread : this is basically called as UI thread where we show the data to the user from the application
  2. Background Thread : This is the thread basically used for the logic calculation or service call at the background.

GCD (Grand Central Dispatch)

GCD (Grand Central Dispatch) is a Low level API basically used to manage the concurrency of the application. It manages the heavy task at the background.

We use DispatchQueue for the operation for the GCD . DispatchQueue is the method object which is used to manage the task execution in serial or concurrent manner basically in FIFO order.

Synchronous and Asynchronous

Synchronous : Execution of task in synchronous manner. here the task are executed one after the another. Untill and unless the 1st task is completed 2nd task is on hold.

GCD execute the task in the form of synchronous and Asynchronous format. In Dispatch Queue Synchronous the task gets executed in the orderly manner .Every task waits until the initial task gets executed and completes it.

// Do the  work synchronously
DispatchQueue.main.sync { ... }

Asynchronous : Execution of task in parallel order . the task 2 is executed even if task 1 is not finished.

Dispatch Queue Asynchronous the task execution’s takes place in unorder manner .The code is run in asynchronous manner

// Do the work asynchronously
DispatchQueue.main.async { ... }

What is Queue ?

Lining up of the task to get executed it can be form of synchronous or Asynchronous format

There are 2 types of Queue

  1. Serial Queue : Serial queue are the queues which are executed one after the another. Task are executed in orderly manner. here task are executed in sequential manner. Execution of task is one at a time.

Example : Consider a following task . Task 1, Task 2 and Task 3 this task has to be executed in serial queues

Task 1 gets executed first then task 2 and then task 3 . Task 2 does not start before Task 1 is completed so same for Task 3 , Task execution takes place in sequential manner .

Pros of serial Queues

  • Predictable Executable order
  • Prevent Race Condition

2.Concurrent Queue : Task are executed in the same order. The execution of code takes place in parallel format.

Example : Consider a following task. Task 1, Task 2 and Task 3 this task has to be executed in Concurrent queues

Task 1, Task 2 and Task 3 are executed in parallel order . Here Task 2 does not wait for Task 1 to be completed . It execute same order as Task 1 and same for Task 3 all the task are executed in parallel format.

Pros of concurrent Queues

  • Faster
  • Unpredictable order

Serial/Concurrent affects the destination queue to which task is dispatching.

Sync/Async affects the current thread from which task is dispatching

There are 2 different types of Dispatch Queue

  1. Main Queue
  2. Global Queue

Main Queue : This is used to execute the task on main thread .this thread mainly used as UI Thread

DispatchQueue.main.async {
     // Perform your async code here

Global Queue: This is used to execute the task on the global thread

DispatchQueue.global().async {
     // Perform your async code here

Quality of Service

  • User Interactive
  • User Initiated
  • Utility
  • Background

User Interactive : it is used when there is user interaction related task or Animation related work takes place

DispatchQueue.global(qos: .userInteractive).async{
    print("User Interactive task")

User Initiated : It is to be used when we required immediate result. basically during the scrolling or searching.

DispatchQueue.global(qos: .userInitiated).async  {
    print("User Initiated task")

Utility : This is used for long running task basically in downloading where user is aware of the proccess which is not at the high priority.

DispatchQueue.global(qos: .utility).async  {
    print("User Utility task")

Background : This is used to run the task at the background. This task are not visible to the user

DispatchQueue.global(qos: .background).async  {
    print("Background thread")

There are 2 more Quality of Service which are rarely used they are

  • Default
  • Unspecified