Returning data from a URLRequest/URLSession response

I made a function (based on examples online) to get that data and it looks something like this:

private func GetResponse(request: URLRequest, completion: @escaping (_ response: String?) -> Void) {
     let task = URLSession.shared.dataTask(with: request) { data, response, error in
          if error != nil {
               // Handle error
               return
          }

          let responseString = String.init(data: data!, encoding: String.Encoding.utf8)

          completion(responseString!)
     }

     task.resume()
}

And it’s used like this:

self.GetResponse(request: request, completion: { (response) in
     if response == "1" {
          // Yes
     } else {
          // No
     }
}

The data appears but I don’t know the best way to return that data. It would be nice to do something like this:

let myData = self.GetResponse(request: request)

Any ideas? Thanks!

Hi @tgregoryknox

As far as i understand you are already obtaining the data in your completion block. The response parameter of your completion closure is the data itself, or am i missing something?
After you got a response you could serialize it as a dictionary and process after that, take a look at this tutorial - https://www.raywenderlich.com/150322/swift-json-tutorial-2
Hope this is helpful.

Nikita

Hello!

The data is being obtained in the block. I guess what I’m really wondering is how to get that response string out of self.GetResponse.

For example, if I wanted to use that value elsewhere in the class. Right now, it seems that I can only access that response string from within the block.

Thanks!

Hi @tgregoryknox

You could just create an instance variable and use it later anywhere in the class:

class YourClass
{
    var data=...
}


self.GetResponse(request: request, completion: { (response) in
.....
 self.data = responce
.....
}

Hope this is helpful.

Nikita

The core issue is: if you’re downloading data from a URL, you don’t want to return that data, because you don’t know how long it will take to download the data, and you don’t want your program to pause while you wait for the download to finish.

The point of the completion handler is that it’s a function which gets called later, when the download has finished.

You need to write your code to account for several possible states:

  • the ‘initialising’ state where the download hasn’t started yet
  • the ‘in progress’ state where the download has started but hasn’t finished
  • one or more ‘error’ states, because the download failed or was interrupted, or the data turned out to be corrupt and unusable, etc.
  • the ‘success’ state where downloading is complete, the data is usable, and you can do something with it (e.g. display it)

Your ‘GetResponse’ call should put your code into ‘downloading in progress’ state, and the completion handler should handle the transition to ‘success’ or ‘error’ states as appropriate. (You’ll be able to determine which state you’re in by looking at the data, response and error.)

This topic was automatically closed after 166 days. New replies are no longer allowed.