I have written this (truncated) code to fetch some tweets:
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSString *JSONStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://search.twitter.com/search.json?q=haak&lang=nl&rpp=100"] encoding:NSUTF8StringEncoding error:nil];
if (!JSONStr) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
return;
}
/*... PARSING ETC ...*/
dispatch_sync(dispatch_get_main_queue(), ^{
[delegate didReceiveTweets:foundTweets];
});
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
});
Note the lines from dispatch_sync(dispatch_get_main_queue(), ^{ to });.
This will update the UI. Is this the good way to do it, or are there better ways than using dispatch_sync within a dispatch_async? Or should I not do this at all?
Should I also send setNetworkActivityIndicatorVisible: from within the main thread?
P.S. The reason I'm not using NSURLConnection is that this code comes from a class-method, so I need to create some object containing the delegate for the NSURLConnection, which seems overkill to me.