Using XCode-8.2.1, Swift-3.0.2 and iOS-10.2.1,
I am trying to call two different URLSession.shared.dataTasks (the first is a simple URL-request and the second is a POST-request).
Since my first dataTask delivers a result that is needed in the httpBody of the second dataTask, the two URLSession.shared.dataTasks shall run in series, one after the other! (and also the preparative code shall run consecutively).
I tried, so far, using two consecutive serialQueue.sync{} queues. But I had to realize that the code does not perform in the order I would like to.
The print-statement in the log turn out to be as follows:
Hmmmmmm 2
Hmmmmmm 1
Hmmmmmm 3
(instead of 1, 2, 3 as needed)!
How can you get the order 1, 2, 3 ??
(i.e. how can you make sure the httpBody of the second dataTask can be filled with a result coming from the first dataTask ?)
Here is my code: (not executable as URL's were taken out - but you get the point)!
import UIKit
class ViewController: UIViewController {
    let serialQueue = DispatchQueue(label: "myResourceQueue")
    var stationID: Int = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.serialQueue.sync {
            let myResourceURL = URL(string: "myQueryString1")
            let task = URLSession.shared.dataTask(with: myResourceURL!) { (data, response, error) in
                if (error != nil) {
                    // print(error.debugDescription)
                } else {
                    if let myData = data {
                        do {
                            let myJson = try JSONSerialization.jsonObject(with: myData, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
                            // print(myJson)
                            // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                            print("Hmmmmmm 1")
                            // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                        } catch {
                            // error
                        }
                    }
                }
            }
            task.resume()
        }
        self.serialQueue.sync {
            var request = URLRequest(url: URL(string: "myQueryString2")!)
            request.httpMethod = "POST"
            request.addValue("API_id", forHTTPHeaderField: "Authorization")
            request.addValue("application/xml", forHTTPHeaderField: "Content-Type")
            // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            print("Hmmmmmm 2")
            // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            let postString: String = "My_XML_POST_Body"
            request.httpBody = postString.data(using: .utf8)
            let task = URLSession.shared.dataTask(with: request) { data, response, error in
                // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                print("Hmmmmmm 3")
                // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            }
            task.resume()
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
Any help appreciated!
 
     
     
    