Omar Kanawati
Omar Kanawati

Reputation: 59

Return object for a method inside completion block

I want to make a method with URL parameter that returns the response of calling that URL. How can I return the data obtained inside a completion block for a method?

class func MakeGetRequest(urlString: String) -> (data: NSData, error: NSError)
{
    let url = NSURL(string: urlString)
    var dataResponse: NSData
    var err: NSError

    let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
           //How can I return the data obtained here....
    })

    task.resume()
}

Upvotes: 2

Views: 4990

Answers (1)

Antonio
Antonio

Reputation: 72760

If you want the MakeGetRequest method to return data obtained via dataTaskWithURL, you can't. That method performs an asynchronous call, which is most likely completed after the MakeGetRequest has already returned - but more generally it cannot be know in a deterministic way.

Usually asynchronous operations are handled via closures - rather than your method returning the data, you pass a closure to it, accepting the parameters which are returned in your version of the code - from the closure invoked at completion of dataTaskWithURL, you call that completion handler closure, providing the proper parameters:

class func MakeGetRequest(urlString: String, completionHandler: (data: NSData, error: NSError) -> Void) -> Void
{
    let url = NSURL(string: urlString)
    var dataResponse: NSData
    var err: NSError

    let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
        completionHandler(data: data, error: error)
    })

    task.resume()
}

Swift 5 update:

class func makeGetRequest(urlString: String, completionHandler: @escaping (Data?, Error?) -> Void) -> Void {
    let url = URL(string: urlString)!
    var dataResponse: Data
    var err: NSError

    let task = URLSession.shared.dataTask(with: url, completionHandler: { (data, respone, error) -> Void in
        completionHandler(data, error)
    })

    task.resume()
}

Upvotes: 4

Related Questions