3

I am new to Swift and I have a question regarding synchronous calls. I would like to make a synchronous call to dataTaskWithRequest, so that the return method is called once the dataTaskWithRequest is finished. Here is my code:

private func sendRequest (request: NSURLRequest) -> NSData{
    let session = NSURLSession.sharedSession()
    var dataReceived: NSData = NSData ()
    let task = session.dataTaskWithRequest(request) { data, response, error in
        if error != nil{
            print("Error -> \(error)")
            return
        }
    dataReceived = data!
    }
    task.resume()
    return dataReceived
}

What is the best way to do it? I have tried with a completion handler but I am not able to do it.

Thank you very much in advance for the help.

user3149877
  • 125
  • 1
  • 1
  • 8

3 Answers3

13

You can turn an asynchronous call synchronous with a semaphore like this:

private func sendRequest(request: NSURLRequest) -> NSData? {
    let session = NSURLSession.sharedSession()
    var dataReceived: NSData?
    let sem = dispatch_semaphore_create(0)

    let task = session.dataTaskWithRequest(request) { data, response, error in
        defer { dispatch_semaphore_signal(sem) }

        if let error = error {
            print("Error -> \(error)")
            return
        }

        dataReceived = data
    }

    task.resume()

    // This line will wait until the semaphore has been signaled
    // which will be once the data task has completed
    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER)
    return dataReceived
}

Then you use the func like this:

let url = NSURL(string: "http://www.google.com")!
let req = NSURLRequest(URL: url)
let data = sendRequest(req)
print("This is data: \(data)")

For Swift 5 The principle is the same, but some items have been renamed. Note this also show more settings for the request.

static func sendRequest() -> Data? {
  let session = URLSession.shared
  var dataReceived: Data?
  let sem = DispatchSemaphore.init(value: 0)

  let params = ["key" : "value"] as Dictionary<String, String>
  let token = ""

  var body = try? JSONSerialization.data(withJSONObject: params, options: [])

  var request = URLRequest(url: URL(string: "https://example.com")!)
  request.httpMethod = "POST"
  request.httpBody = body
  request.addValue("application/json", forHTTPHeaderField: "Content-Type")
  request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")

    let task = session.dataTask(with: request) { data, response, error in
        defer { sem.signal() }

        if let error = error {
            print("Error -> \(error)")
            return
        }


        dataReceived = data
    }

  task.resume()

  // This line will wait until the semaphore has been signaled
  // which will be once the data task has completed
  sem.wait()

  return dataReceived
}

To use it

let data = sendRequest()
tim.baker
  • 2,807
  • 4
  • 24
  • 48
hola
  • 2,180
  • 9
  • 21
3

completion is the right and the best way in swift!

  func sendRequest (request: NSURLRequest,completion:(NSData?)->()){

    NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
        if error != nil{
            return completion(data)
        }else{
            return completion(nil)
        }
    }.resume()
}

and call:

  sendRequest( yourRequest ) { data in
        if let data = data {
             // do something
        }
   }}
1

The best way to do this is a callback function.

private func sendRequest (request: NSURLRequest, callback: (data: NSData) -> () {
    let session = NSURLSession.sharedSession()
    var dataReceived: NSData = NSData ()
    let task = session.dataTaskWithRequest(request) { data, response, error in
        if error != nil{
            print("Error -> \(error)")
            return
        }
        callback(data!)
    }
    task.resume()
}

You can call this function like this

self.sendRequest(request, { (data) in 
    //data is the data you get from the request
}
Yannick
  • 2,954
  • 1
  • 17
  • 29