last day (21 days later) » 

10:36 PM
1
A: Swift: How do I return a value within an asynchronous urlsession function?

RobYou should add your own completionHandler closure parameter and call it when the task completes: func googleDuration(origin: String, destination: String, completionHandler: (Int?, NSError?) -> Void ) -> NSURLSessionTask { // do calculations origin and destiantion with google distance matrix ...

 
Is there any way I could make this non-asynchronous? I need it to wait until there is a response parsed but I don't know how to do it. Your solution was right, I think my question was just wrong
 
Rob
You can, but it's seriously frowned upon. It's a horrible UX and if you do it at the wrong time, the watchdog process might summarily kill your app. The correct solution is to disable the UI, present spinner (in iOS, a UIActivityIndicatorView), and reverse that process when the request is done. You really should embrace the asynchronous pattern rather than fighting it.
By the way, often people will dispatch those completion handlers to the main queue (because the caller is usually engaging in UI updates). If you want me to update answer to show you how to do that, let me know. It's probably self explanatory if you're proficient with GCD, but I'm happy to show you, if you're not.
 
If I do this, it sets timeLabel as -999,not what I want.var tripTime = -999; googleDuration(startLabel.text, destination: endLabel.text) { totalTime, error in if let totalTime = totalTime { // use totalTime here tripTime = totalTime; println(tripTime); } } println("Returning: "); println(tripTime); timeLabel.text = String(tripTime);
 
Rob
Move the setting of the timeLabel.text inside the closure rather than after it. (But like I said, dispatch that to the main queue.)
 
it says it requires explicit 'self.', but when I do that, it doesn't assign the value
 
Rob
10:36 PM
Yep, so it's self.timerLabel.text = .... Did you dispatch this to the main queue?
 
so if I have `@IBAction func calcTime(sender: UIButton) {
self.view.endEditing(true); // dismiss keyboard
timeLabel.hidden = false; // show timeLabel

// duration function call
googleDuration(startLabel.text, destination: endLabel.text) { totalTime, error in
if let totalTime = totalTime {
self.timeLabel.text = String(totalTime);
} else {
// error handling
}
}

}`
it doesn't set it
it doesn't seem to do anything within if let totalTime = totalTime {}
 
Rob
11:00 PM
Can you paste that in again, this time hitting "fixed font" button before hitting send?
I think I can make it out.
Can you confirm that it's hitting that line which is setting the label (via breakpoint or log statement)?
@teo751 let me know what you find.
@teo751 And, again, did you dispatch this to the main queue?
By the way, are you confident that your network request and JSON parsing are succeeding?
I've got people,coming over tonight, but I'll check in after the leave...
... After they leave.
 
11:26 PM
I'm sure my network request and json parsing work - how do i dispatch to main queue?
 
Rob
dispatch_async(dispatch_get_main_queue()) { ... }
 

  last day (21 days later) »