Reputation: 415
I'm loading a bunch of files through Alamofire requests. I want to show a "loading…" spinner (MBProgressHUD) while they load.
But I'm having some trouble determining when all the requests have finished, so that I can hide the HUD at the correct time!
So far, everything I've tried has resulted in the hud being hidden too early, or never at all. Currently, I have my requests wrapped in an NSOperation subclass, with a simple NSBlockOperation to hide the hud, which has all the Alamofire request operations as dependencies. But I can't figure out how to get the requests to be marked as finished at the right time.
Ideally I'd like to find a solution that's simpler than this. What would be the best way to accomplish this? Thanks.
Upvotes: 2
Views: 454
Reputation: 16466
use disptach_group_t
Here is Example
dispatch_group_t group = dispatch_group_create();
__weak MainViewControllerSupplier * weakSelf = self;
dispatch_group_enter(group);
[self showHUD];
[[DataManager sharedManager] getCategoriesWithSuccessBlock:^(NSArray *categories) {
weakSelf.arrCategories = categories;
dispatch_group_leave(group);
// NSLog(@"response category= %@",categories);
} failureBlock:^(NSError *error) {
NSLog(@"response category = %@",error);
dispatch_group_leave(group);
}];
dispatch_group_enter(group);
[[DataManager sharedManager] getRegionsWithSuccessBlock:^(NSArray *regions) {
weakSelf.arrRegions = regions;
dispatch_group_leave(group);
} failureBlock:^(id error) {
NSLog(@"response region = %@",error);
dispatch_group_leave(group);
}];
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
dispatch_async(dispatch_get_main_queue(), ^{
[self hideHUD];
});
// All task completed
});
Upvotes: 2