Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift wait until dataTaskWithRequest has finished to call the return [duplicate]

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.

like image 352
user3149877 Avatar asked Dec 01 '22 16:12

user3149877


1 Answers

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()
like image 197
hola Avatar answered Dec 10 '22 21:12

hola