在继续之前等待多个Alamofire请求完成?

时间:2023-01-06 19:39:06

I'm loading a bunch of files through Alamofire requests. I want to show a "loading…" spinner (MBProgressHUD) while they load.

我正在通过Alamofire请求加载一堆文件。我想在加载时显示“loading ...”微调器(MBProgressHUD)。

But I'm having some trouble determining when all the requests have finished, so that I can hide the HUD at the correct time!

但是我在确定所有请求何时完成时遇到了一些麻烦,因此我可以在正确的时间隐藏HUD!

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.

到目前为止,我尝试过的所有事情都导致过早隐藏,或者根本不隐藏。目前,我的请求包含在NSOperation子类中,使用简单的NSBlockOperation来隐藏hud,它将所有Alamofire请求操作都作为依赖项。但我无法弄清楚如何在正确的时间将请求标记为已完成。

Ideally I'd like to find a solution that's simpler than this. What would be the best way to accomplish this? Thanks.

理想情况下,我想找到一个比这更简单的解决方案。实现这一目标的最佳方法是什么?谢谢。

1 个解决方案

#1


2  

use disptach_group_t Here is Example

使用disptach_group_t这是示例

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 

});

#1


2  

use disptach_group_t Here is Example

使用disptach_group_t这是示例

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 

});