Asynchronous Operations with Grand Central Dispatch in SWIFT !

Grand Central Dispatch

We call them Global Dispatch queues. These queues are global to the application and are differentiated only by their priority level. To use one of the global concurrent queues, you have to get a reference of your preferred queue using the function dispatch_get_global_queue which takes in the first parameter one of these values:

DISPATCH_QUEUE_PRIORITY_HIGH
DISPATCH_QUEUE_PRIORITY_DEFAULT
DISPATCH_QUEUE_PRIORITY_LOW
DISPATCH_QUEUE_PRIORITY_BACKGROUND

These queue types represent the priority of execution. The queue with HIGH has the highest priority and BACKGROUND has the lowest priority. So you can decide the queue you use based on the priority of the task. Please also note that these queues are being used by Apple’s APIs so your tasks are not the only tasks in these queues.

Using Concurrent Dispatch Queues

This method handles the image download. We now perform the task like this:

@IBAction func didClickOnStart(sender: AnyObject) {
let FirstImage = Downloader.downloadImageWithURL(imageURLs[0])
self.imageView1.image = FirstImage

let SecondImage = Downloader.downloadImageWithURL(imageURLs[1])
self.imageView2.image = SecondImage

let ThirdImage = Downloader.downloadImageWithURL(imageURLs[2])
self.imageView3.image = ThirdImage

let FourthImage = Downloader.downloadImageWithURL(imageURLs[3])
self.imageView4.image = FourthImage

}

Each downloader is considered as a task and all tasks now are being performed in the main queue. Let’s now get a reference for one of the global concurrent queue which is the Default priority queue.

let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(queue) { () -> Void in

let img1 = Downloader.downloadImageWithURL(imageURLs[0])
dispatch_async(dispatch_get_main_queue(), {

self.imageView1.image = img1
})

}

We first get a reference to the default concurrent queue using dispatch_get_global_queue, then inside the block we submit a task which is to download the first image. Once the image download completes, we submit another task to the main queue to update the image view with the downloaded image. In other words, we put the image download task in a background thread, but execute the UI related tasks in the main queue.

Using Serial Dispatch Queues

@IBAction func didClickOnStart(sender: AnyObject) {

let serialQueue = dispatch_queue_create("com.mycodetips.imagesQueue", DISPATCH_QUEUE_SERIAL)


dispatch_async(serialQueue) { () -> Void in

let FirstImage = Downloader .downloadImageWithURL(imageURLs[0])
dispatch_async(dispatch_get_main_queue(), {

self.imageView1.image = FirstImage
})

}
dispatch_async(serialQueue) { () -> Void in

let SecondImage = Downloader.downloadImageWithURL(imageURLs[1])

dispatch_async(dispatch_get_main_queue(), {

self.imageView2.image = SecondImage
})

}
dispatch_async(serialQueue) { () -> Void in

let ThirdImage = Downloader.downloadImageWithURL(imageURLs[2])

dispatch_async(dispatch_get_main_queue(), {

self.imageView3.image = ThirdImage
})

}
dispatch_async(serialQueue) { () -> Void in

let FourthImage = Downloader.downloadImageWithURL(imageURLs[3])

dispatch_async(dispatch_get_main_queue(), {

self.imageView4.image = FourthImage
})
}

}

Discover more from mycodetips

Subscribe to get the latest posts sent to your email.

Discover more from mycodetips

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top