# Returning data from a URLRequest/URLSession response

**URL:** <https://forums.kodeco.com/t/returning-data-from-a-urlrequest-urlsession-response/35996>\
**Category:** Swift\
**Created:** [October 27, 2017, 2:57pm UTC](https://forums.kodeco.com/t/returning-data-from-a-urlrequest-urlsession-response/35996 "2017-10-27T14:57:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tgregoryknox](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/tgregoryknox/32/72839_2.png) [@tgregoryknox](https://forums.kodeco.com/u/tgregoryknox)\
**Post date:** [October 27, 2017, 2:57pm UTC](https://forums.kodeco.com/t/returning-data-from-a-urlrequest-urlsession-response/35996/1 "2017-10-27T14:57:56Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![nikita\_gaydukov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/nikita_gaydukov/32/77683_2.png) [@nikita\_gaydukov](https://forums.kodeco.com/u/nikita_gaydukov)\
**Post date:** [October 27, 2017, 3:45pm UTC](https://forums.kodeco.com/t/returning-data-from-a-urlrequest-urlsession-response/35996/2 "2017-10-27T15:45:04Z")

</div>

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](https://www.raywenderlich.com/150322/swift-json-tutorial-2)  
Hope this is helpful.

Nikita

---

<div class="post-metadata">

**Author:** ![tgregoryknox](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/tgregoryknox/32/72839_2.png) [@tgregoryknox](https://forums.kodeco.com/u/tgregoryknox)\
**Post date:** [October 27, 2017, 5:52pm UTC](https://forums.kodeco.com/t/returning-data-from-a-urlrequest-urlsession-response/35996/3 "2017-10-27T17:52:51Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![nikita\_gaydukov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/nikita_gaydukov/32/77683_2.png) [@nikita\_gaydukov](https://forums.kodeco.com/u/nikita_gaydukov)\
**Post date:** [October 30, 2017, 4:06pm UTC](https://forums.kodeco.com/t/returning-data-from-a-urlrequest-urlsession-response/35996/4 "2017-10-30T16:06:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![narrativium](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/narrativium/32/110937_2.png) [@narrativium](https://forums.kodeco.com/u/narrativium)\
**Post date:** [October 30, 2017, 6:28pm UTC](https://forums.kodeco.com/t/returning-data-from-a-urlrequest-urlsession-response/35996/5 "2017-10-30T18:28:37Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [April 12, 2018, 6:58am UTC](https://forums.kodeco.com/t/returning-data-from-a-urlrequest-urlsession-response/35996/6 "2018-04-12T06:58:05Z")

</div>

Closed on April 12, 2018, 6:58am UTC

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