I am trying to run a following Function
+(NSString *)responseFromQuery:(NSString *) queryString
{
__block NSString *response;
__block NSError *err = nil;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),^{
response = [NSString stringWithContentsOfURL:[NSURL URLWithString:queryString] encoding:NSUTF8StringEncoding error:&err];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"%@",response);
if (err != nil)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert!"
message:@"An Error has occurred" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alertView show];
return response;// = @"Error";
}
});
});
// return response;
}
I want to pass the value of response after executing the block, however instead it runs the last return first , how can i make the return expression within block to run.
I am using this function in an NSObject Class
According to the debugging it never runs the dispatch_async queue.
Right now if i return it gives me error "Control may reach at the end of non void block"
To fully understand the implementation and working of blocks in objective c i think you should look into the following link, it explains quite a lot and it is put really simply.
Objective C Blocks – Simple Understanding