Group Group Group Group Group Group Group Group Group Forums

Grand Central Dispatch Tutorial for Swift 3: Part 2/2

Get up to speed with concurrency, threading and parallelism in this in-depth two-part Grand Central Dispatch tutorial for iOS.

This is a companion discussion topic for the original entry at

Thank you for the explanation of the GCD, I really appreciate that. I have a question on section Concurrency Looping. What does the following line does?

let _ = .userInitiated)

1 Like

This is great stuff. Exceptional. I started on this “thread of GCD” with closures; my situation is like that of of a network async behavior but where operation completion is completed in a different method. While I knew I’d be using GCD, I expected to be using “escape” closures. Here is my first question: isn’t some of the synchronization (dispatch_group) a replacement for what could be an escape closure?

Second, and my comments are meant as feedback, introducing semaphores at the end while obviously related to synchronization is tangential to your target. GCD and dispatch is right on. Why is that? Answer: my experience is that the lesson here isn’t easily transferred to say command line. Oh, I know it compiles, but the lack of the iOS ‘main thread’ and managed memory will require more work. Consider the application programmer that wants to use these lessons (knowing only swift as their model of execution) across the systems (command line, cocoa, and then iOS). Isn’t GCD and dispatch meant to replace the old-school synchronization objects?


func downloadData(){
    let url = NSURL(string: baseURL)!
    let request = NSURLRequest(url: url as URL)
    let session = URLSession.shared
    // let task = session.dataTask(with: request) { (data, response ,errror) -> Void in
    let task = session.dataTask(with: request as URLRequest!){ (data, response ,error) -> Void in
        if error != nil {
                let dict = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? Dictionary<String, AnyObject>
                // Parsing data
                if let results = dict!["results"] as? [Dictionary<String,AnyObject>] {
                 //  print(results)
                    for obj in results {
                        let movie = Movie(movieDict: obj)

how do i write unit test cases fot testing response,error,statuscode and also JSONdata

this is a method written in ViewController class and also in MVC format using ASYN technique

I have an iOS app written in Swift 3. in one of the modules i need to upload several files into a Web Server concurrently (files are chosen by the user from camera or gallery). i also have to display progress indicator for each file that indicates when upload is in progress and when upload is completed. the files are about 3-4MB each and the number of files is unknown (depends on the user) .

My Question:

should i use the low level GCD api or the higher NSOperation/NSOperationQueue abstraction ? I have searched a lot but some got mixed answers (some say GCD and other say NSOperationQueue).i’v been struggling with this question a lot and i don’t want to start coding only to find out in the end that i picked the wrong approach. (multi-threaded code is challenging anyhow)

Thank you very much!

Hi, accord to this mail list thread, it is done to specify the queue that will be used by concurrentPerform, but I just tested it and use another, I don’t know how it’s selected because is not even the same queue where concurrentPerform was called. I also would like to know certainly what is this for and how to specify a queue to use.

This is meant to specify the Quality of Service class to use, matching the prior steps.

@pistach, I just read up on escape closures. It does look very similar to Dispatch Group. So here I’d agree with you, you could probably use that instead. It’s worth trying. I didn’t quite get your second piece of feedback. Could you elaborate further?

dannofx, it’s my understanding that this would set the QoS which in turn would guide GCD into picking a high priority queue. What queue did you see being used?

I would start with the higher abstractions unless you get stuck and really need to delve into the lower level APIs. If you haven’t already made a call on the direction to go, that would be my suggestion.

If you’re still struggling with this, it would be helpful to know what you’ve tried.

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]