Kodeco 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 https://www.raywenderlich.com/742-grand-central-dispatch-tutorial-for-swift-3-part-2-2

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 _ = DispatchQueue.global(qos: .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?

hi

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 {
            print(error.debugDescription)
            }
            
        else{
            do{
                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)
                        self.moviesfeat.append(movie)
                    }
                    
                    DispatchQueue.main.async(){
                        
                        self.collect2.reloadData()
                    }
                    
                }
            }
            catch{
                
            }
            
            
        }
        
    }
    task.resume()
}

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

FYI
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! :]