A couple of things:
The key observation is to always use dispatch_async
unless you need dispatch_sync
. You don't need synchronous operation here, so just use dispatch_async
for your UI updates.
If you're running addProcess
from the main queue, it doesn't need to dispatch the first UI update back to the main queue. Obviously, if you're running this from a background queue, you do.
The original question had the dispatch to the background queue within addProcess
, which makes more sense to me (keeps all the GCD stuff nicely encapsulated). You've updated your answer to say that you're invoking this via dispatch_async([self addProcess])
(by which I presume you meant to a global queue, not the main queue), which I'm less crazy about. I address both scenarios in my code samples below.
So, in short, if you're invoking this via [self addProcess]
(without dispatching that, itself, to the background queue) I'd suggest:
- (void) addProcess:(NSString *)searchTerm
{
UIApplication *app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
[SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeGradient];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//DO SOME VERY LONG STUFF HERE
dispatch_async(dispatch_get_main_queue(), ^{
UIApplication *app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = NO;
[SVProgressHUD dismiss];
});
});
}
Or, alternatively,
- (void) addProcess:(NSString *)searchTerm
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
UIApplication *app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
[SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeGradient];
});
//DO SOME VERY LONG STUFF HERE
dispatch_async(dispatch_get_main_queue(), ^{
UIApplication *app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = NO;
[SVProgressHUD dismiss];
});
});
}
And if you're doing ...
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self addProcess];
});
then it's just:
- (void) addProcess:(NSString *)searchTerm
{
dispatch_async(dispatch_get_main_queue(), ^{
UIApplication *app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
[SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeGradient];
});
//DO SOME VERY LONG STUFF HERE
dispatch_async(dispatch_get_main_queue(), ^{
UIApplication *app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = NO;
[SVProgressHUD dismiss];
});
}