What is Concurrency?
Concurrency is a process in which a program can perform multiple tasks simultaneously, executing tasks or processes in the background. The concurrency turns the decomposability of program, algorithm or problem into order-independent or partially ordered components or units.
Why use Concurrency?
It's critical to ensure that your app runs as smoothly as possible to ensure that user wait time is reduced. For example, scrolling through a table of images is one of the more common situations in which an end-user is impacted by a lack of concurrency. If you need to download images from a network or perform some type of image processing before displaying it, the scrolling will be stuck and you will be forced to display multiple indicators instead of an expected image.
How to use Concurrency?
At a high level, you need to structure your app so that some tasks can run at the same time. Multiple tasks that modify the same resource (i.e. variable) can't run at the same time unless you make them thread-safe.
Tasks which access different resources, or read-only shared resources, can all be accessed via different threads to allow for much faster processing.
There are two API's you will use when making your concurrent:
- Grand Central Dispatch (GCD)
- Operations
Grand Central Dispatch
GCD is Apple's implementation of C's libdispatch library. Its purpose is to queue up tasks, either method or closure, that can run in parallel depending or the availability of resources. It then executes the tasks on an available processor core.
GCD's tasks are so lightweight to enqueue that Apple, in its GCD technical brief, stated that only 15 instructions are required for implementation, whereas creating traditional threads could require several hundred instructions.
All of the tasks GCD manages for you are placed into GCD first-in, first-out(FIFO) queues. Each task that you submit to a queue is then executed against a pool of threads fully managed by the system.
Declaration -
- DispatchQueue.main.async {
-
-
- }
DispatchQueue - An Object that manages the execution of tasks serially or concurrently on your app's main thread or background thread.
Note
There is no guarantee as to which thread your task will execute against.
Synchronous and Asynchronous task
Work is placed on queue either synchronously or asynchronously. When running a task synchronously, your app will wait and block the current run loop until execution finishes before moving on to the next task. A task that runs asynchronously will start and return execution immediately. This way, the app is free to run other tasks while the first one is executing.
Note
It's important to keep in mind that while the queue is FIFO based, it does not ensure that tasks will finish in the order you submit them. The FIFO procedure applies to when the task starts, not when it finishes.
Using Concurrent Dispatch Queue
Let's take an example using concurrent dispatch - the below example handles image download from URLs:
- import UIKit
-
- let imageURLs = [
- "https://www.planetware.com/photos-large/F/france-paris-eiffel-tower.jpg",
- "https://adriatic-lines.com/wp-content/uploads/2015/04/canal-of-Venice.jpg",
- "https://www.planetware.com//wpimages/2019/09/connecticut-mystic-top-things-to-do-mystic-river-bascule-drawbridge-downtown-mystic.jpg",
- "https://www.planetware.com/photos-large/CAY/best-luxury-all-inclusive-world-nihi-sumba.jpg"
- ]
-
- class ViewController: UIViewController {
- @IBOutlet weak var imgFirst: UIImageView!
- @IBOutlet weak var imgSecond: UIImageView!
- @IBOutlet weak var imgThird: UIImageView!
- @IBOutlet weak var imgFourth: UIImageView!
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- }
-
- @IBAction func didStartAction(_ sender: Any) {
- let queue = DispatchQueue(label:"com.queue.concurrencyDemo")
- queue.async{
- let firstImg = Downloader.downloadImageWithURL(url: imageURLs[0])
- DispatchQueue.main.async {
- self.imgFirst.image = firstImg
- }
- }
- queue.async{
- let img2 = Downloader.downloadImageWithURL(url: imageURLs[1])
- DispatchQueue.main.async {
- self.imgSecond.image = img2
- }
- }
-
- queue.async {
- let img3 = Downloader.downloadImageWithURL(url: imageURLs[2])
- DispatchQueue.main.async {
- self.imgThird.image = img3
- }
- }
-
- queue.async{
- let img4 = Downloader.downloadImageWithURL(url: imageURLs[3])
- DispatchQueue.main.async{
- self.imgFourth.image = img4
- }
- }
- }
-
- }
-
- class Downloader {
- class func downloadImageWithURL(url:String) -> UIImage! {
- if let data = NSData(contentsOf: NSURL(string: url)! as URL){
- return UIImage(data: data as Data)
- }
- return UIImage.init()
- }
- }
I hope you understood Concurrency using Grand Central Dispatch Queue(GCD). Soon, I will write an article about NSOperationQueue to achieve concurrency.
Thanks, Cheers!!