-1

Please can you tell me how to return [data] out of a session.uploadTask in oder to make [data] available for multiple ViewControllers?

My classes:

class data {
  var name1: String = ""
  var name2: String = ""
  ...
}

class fetchData {
  var url: String = ""
  var body: String = ""
  ... 

 func getDataFromServer (apiParrameters,...) -> [data] {
    var dataArray = [data]()     
    session.uploadTask() {
    // fetching the [data
    ...
    // adding [data]
    ... dataArray.append(data(name1: name1String, name2: name2String)) ...

   //Where to retun(dataArray)? In session.uploadTask it is not possible/accespted
   /* in OperationQueue.main.addOperation({
        //return(dataArray) is not possible
        })*/
 }
 resume(uploadTask)
 //return(dataArray) at this point results in an empty dataArray


 }
}
Jim
  • 13
  • 4
  • Have done research, for sure. Otherwise I would nit ask...Can you provide a link? – Jim Mar 01 '17 at 11:59
  • Possible duplicate of [Swift: How do I return a value within an asynchronous urlsession function?](http://stackoverflow.com/questions/27081062/swift-how-do-i-return-a-value-within-an-asynchronous-urlsession-function) – Eric Aya Mar 01 '17 at 12:52

1 Answers1

0

The completion handler to call when the load request is complete. This handler is executed on the delegate queue.This completion handler takes the following parameters:

data The data returned by the server. response An object that provides response metadata, such as HTTP headers and status code. If you are making an HTTP or HTTPS request, the returned object is actually an HTTPURLResponse object. error An error object that indicates why the request failed, or nil if the request was successful.

Alok Kumar
  • 66
  • 1
  • 7