I have a function that download data using Alamofire and then I would like to return that data. Now I know that Alamofire runs asynchronously and in order to to return data I should use completionHandler
, however I don't get it how to use it. Since I am not the first with such problem, I found some solutions to similar problems, yet I am not sure to apply them to my case. Here is my code:
func downloadImageFromServer(imageUrl: String) -> (String, String) {
var myData1 = String()
var myData2 = String()
Alamofire.request(.GET, imageUrl)
.responseImage { response in
switch response.result {
case .Success:
if let newImage = response.result.value {
myData1 = //returned image name
myData2 = //edited image name
}
case .Failure:
//Do something
}
}
return (myData1, myData2)
}
Should I do something like this:
func downloadImageFromServer(imageUrl: String, completionHandler: (String?, String?) -> ()) {
var myData1 = String()
var myData2 = String()
Alamofire.request(.GET, imageUrl)
.responseImage { response in
switch response.result {
//if user does have a photo
case .Success:
myData1 = //Something
myData2 = //Something else
completionHandler(myData1 as? String, myData2 as? String)
case .Failure:
//Print error
}
}
}
Update Yes, my question is very similar to that other question, however in my case, code has to return 2 values and that's where I find difficulties. Here's my code for gettin values:
func getImages(orders: String, completionHandler: (String?, String?) -> ()) {
justDoIt(orders, completionHandler: completionHandler)
}
and then
getImages(imgURL) { responseObject, error in
print(responseObject)
return
}
And it works, however I am able to access only first value of the two, how to access both?