Re: Return from a background queue
Re: Return from a background queue
- Subject: Re: Return from a background queue
- From: Roland King <email@hidden>
- Date: Wed, 23 Dec 2015 00:39:41 +0800
You can’t do what you’re trying to do.
First off, a block (which is what dispatch_async() takes) is defined to take no parameters and return nothing. That’s the source of your compilation error, you’re trying to return something from a block defined not to return anything.
Secondly, what you’re trying to do doesn’t make sense, you are trying to return a value from a method which enqueues work for later execution. You can’t return the return value of that block because at the time you return, the block hasn’t been executed. Your code is basically this
func thing()->Banana
{
dispatch_async( dispatch_get_main_queue() )
{
()->Banana in
return Banana( curveyness: 1.0, smellsGross: true )
}
}
The dispatch_async queues the block and instantly returns, at this point there is no Banana, the banana will be created later. dispatch_async() returns nothing, there is nothing to return. When the banana is eventually created and returned, it’s returned into nothingness, the method which called it long-since finished.
You need your method to create its banana and then call asynchronously back to another method which consumes it. You can’t synchronise asynchronous calls like that.
> On 22 Dec 2015, at 22:25, Eric E. Dolecki <email@hidden> wrote:
>
> 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
_______________________________________________
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