Return from a background queue
Return from a background queue
- Subject: Return from a background queue
- From: "Eric E. Dolecki" <email@hidden>
- Date: Tue, 22 Dec 2015 14:25:28 +0000
I am trying to return a dictionary from a method that uses a queue. I'm not
sure how to actually return though as I get an error (can't convert value
of type '() -> Dictionary<String,String>' to expected argument type
'dispatch_block_t'. I've tried a number of things but none prevent an error
somewhere. I could ditch the queue altogether, but it feels better to use
one.
- Eric
func getForecast(cityName:String) -> Dictionary<String,String> {
let baseUrl: String = "
http://api.openweathermap.org/data/2.5/forecast"
let url: String =
"\(baseUrl)?q=\(theCityName)&units=imperial&appid=\(APIKey)"
let finalUrl: NSURL = NSURL(string: url)!
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(finalUrl, completionHandler:
{data, response, error -> Void in
if error != nil{
print(error!.localizedDescription)
}
var err: NSError?
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue =
dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
let json = JSON(data: data!, options:
NSJSONReadingOptions(), error: &err)
//print("response is \(json)")
let weatherCurrent = json["list"][0]["main"]["temp"]
let weatherHigh = json["list"][0]["main"]["temp_max"]
let weatherLow = json["list"][0]["main"]["temp_min"]
let conditionForecast =
json["list"][0]["weather"][0]["description"].stringValue
* dispatch_async(dispatch_get_main_queue(), { () ->
Dictionary<String,String> in*
* print("Forecast: Current: \(weatherCurrent)ºF.
H:\(weatherHigh), L:\(weatherLow). Cond:
\(conditionForecast.capitalizedString).")*
* let dict = ["current":"\(weatherCurrent)",
"high":"\(weatherHigh)",*
* "low":"\(weatherLow)",
"conditions":"\(conditionForecast.capitalizedString)"]*
* return dict*
})
})
})
task.resume()
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden