Memory

Swift Concurrency: Introduction

What is concurrency? Logic of your app to determine which pieces can run at the same time, and possible in random order, yet still result in a correct implementation of your data flow. Example: You have 10 images on an...

J
Joynal Abedin
6
Swift Concurrency: Introduction

What is concurrency?

Logic of your app to determine which pieces can run at the same time, and possible in random order, yet still result in a correct implementation of your data flow.

Example: You have 10 images on an iPhone screen but total number of images are 100, through JSON API. Some people put an indicator in the center to load the images. That indiator is our UI part. The main part is our UI part and we do all the logic in background part. If you have done th logic in the main part your application is going to be freeze because it can not perform all operations together. So, we put our UI into the main. We put an indicator in the center for loading images, or In the bottom when scrolling images(Instagram). It depends on data, you are loading , so we need to manage it.

How will it work?

Threading will help you to determine the task is running whether single or multiple at a time.

Modern devices almost always have more than a single CPU, and Apple's iPhones have been dual core since 2011. Having more than one core means they are capable of running more than a single task at the same time. By splitting your app into logical \"chunks\" of code you enable the iOS device to run multiple parts of your program at the same time, thus improving overall performance.

Why use concurrency?

It's critical to ensure that your app runs as smoothly as possible and that the end user is not ever forced to wait for something to happen.

However , if a human has to wait a second to see a response after taking an action on a device like an iPhone, if feels like an eternity. \"It's too slow\" is one of the main contributors to your app being uninstalled. It's true, right?

Concurrency save us from all these problems and runs our app as smoothly as possible.

There is a screen, in which there is a single dispatch, and In another screen which consists more than single dispatch.

What will happen after so many redirections?

If you redirected to a screen, where there is a multiple dispatch. Then this app is going to be hang because when we redirect to a screen with multiple task, it will perform multiple tasks at the same time, without completing the single task first and it will not update the UI until the background task finishes. This is the reason why your app got hanged.

You must know, your background task should be finished before you enter into the new screen. It will make the app smoother. In other words, it will prevent  your app being uninstalled.

Scrolling through a table of images is one of the more common situations wherein the end user will be impacted by the lack of concurrency. If you need to download an image from the network, or perform some type of image processing before displaying it, the scrolling will stutter and you will be forced to display multiple \"busy\" indicators instead of the expected image.

A beneficial side effect to using concurrency is that it helps you to spend a bit more time thinking about your app's overall architecture. Instead of just wring massive theods to get the job done you will find yourself naturally wring smaller, more manageable methods that can run concurrently.

Instead of just writing massive methods, Concurrency helps to build app's overall architecture. There are so many things to learn. It is not limited only MVC, MVVM.

It is always better to know where to pt logic code and UI part, when you work with nested dispathch. If you make a mistake of switching these two parts, It will be a problem.

How to use concurrency?

Two main ways to code concurrently. First one is GCD and the second way is Operation class.

I will focus on the two main ways that iOS provides you with the ability to run code concurrently. First isGrand Central Dispatch will cover the common scenarios where you will find yourself being able to implement concurrency. You will learn how to run tasks in the background, how to group tasks together and how to handle restricting the number of tasks that can run at once.

The second section you will focus on the Operation class. Build on top of Grand Central Dispatch, operations allow for the handling of more complex scenarios such as reusable code to be run on a background thread, having one thread depend on another, and even canceling an operation before it's started or completed.

The dispatch Queue and it's quality of services are included in GCD.

In Operation, we can give priorities to operations. In Operation Queue, you can cancel the task at any time and you can also know whether the operation is finished or not?. GCD and Operation queue both have their advantages. Use either one depending on your requirement.

In operation queue, You could perform Start, Finish, Cancel and Execution Operations. Rest of the operations are performed using GCD.

J

Written by Joynal Abedin

Passionate about technology, code, and sharing knowledge.

0 Comments

Leave a Comment