I have a problem with setting value to NSString inside block.
__block NSString *cityID = [[NSString alloc] init];
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) {
cityID = [NSString stringWithFormat:@"%@",[[json valueForKeyPath:@"location"] valueForKey:@"id"]];
NSLog(@"city id for 621352674 = %@",cityID);
} failure:nil];
[operation start];
NSLog(@"city id for 621352674 = %@",cityID);
First NSLog shows me right value of cityID. But second shows me nothing. How to ix this problem?
The second
NSLog()isn’t waiting for the block to complete. That’s actually the point of the operation: it’s running in the background so that the main thread and thus the UI of your app don’t lock up while data is being fetched over the network. The value of that string hasn’t changed when you get to that call immediately after you start the operation. If you change the literal parts of the strings in theNSLog()s:it may become clearer to you what’s happening.