The line of code inside blocks in objective c is executed later after executing other lines of code in the same method. My query is:
There is a method named :
-(NSDictionary*)callingWeatherService{
NSString *urlString = @"http://api.openweathermap.org/data/2.5/weather?q=London,uk";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// 3
self.dictionary = (NSDictionary *)responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// 4
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}];
// 5
[operation start];
return self.dictionary;
}
But the statement return self.dictionary;
gets executed first before block execution so dictionary is returned as nil
;
Is there any solution that statement return self.dictionary;
is executed only after block execution. I need this approach for a specific purpose.
Don't want to use delegates, I had already used delegates inside afnetworking blocks for getting dictionary data.