Latest Post

Concurrency in iOS swift Python sort() Function: A Faster and Essential Way to Sort a List in Python Reading a JSON File in Python: Best Ways to Read JSON File in Python

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

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.

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

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.

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

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 { ... }

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 { ... }

Leave a Reply

Your email address will not be published.