iOS学习专题

同一页面多次网络请求MBProgressHUD使用的另一种方案

2018-08-16  本文已影响118人  口子窖

有时候我们会遇到在同一个页面发起多个网络请求的情况。普遍的做法是采用dispatch_group,当组内的所有方法都完成之后执行reloadData,达到只显示一次加载动画的效果。
先来看看代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.progressHUD showAnimated:YES];
    dispatch_group_t group = dispatch_group_create();
    [self request1:group];
    [self request2:group];
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        //所有请求完成,结束动画
        [self.progressHUD hideAnimated:YES];
    });
}

- (void)request1:(dispatch_group_t)group {
    dispatch_group_enter(group);
    [manager GET:@"https://xxx.json" parameters:@{} progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        dispatch_group_leave(group);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        dispatch_group_leave(group);
    }];
}

- (void)request2:(dispatch_group_t)group {
    dispatch_group_enter(group);
    [manager GET:@"https://xxx.json" parameters:@{} progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        dispatch_group_leave(group);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        dispatch_group_leave(group);
    }];
}

这种做法,在网络请求不是很多,并且网速良好的情况,用户体验很好,但是当网络请求很多,同时网络差的时候,用户会一直看到MBProgressHUD的等待动画。我们希望,在加载完一个请求之后,展示UI,同时MBProgressHUD的等待动画继续,以此类推,当所有的请求完成之后,结束MBProgressHUD等待动画。

下面是代码演示,代码很粗糙,大家可以根据思想自己去实现。

#import <Foundation/Foundation.h>
#import "MBProgressHUD.h"
@interface IdiotSentinelProgressHUD : NSObject
+ (void)showHUDAddedTo:(UIView *)view animated:(BOOL)animated;
+ (void)showHUDAddedTo:(UIView *)view withCount:(NSInteger)count animated:(BOOL)animated;
+ (void)hideHUDForView:(UIView *)view animated:(BOOL)animated endBlock:(void (^)(NSInteger i))end;
+ (void)finalHideHUDForView:(UIView *)view animated:(BOOL)animated endBlock:(void (^)(NSInteger i))end;
@end

@interface UIView (Sentinel)

/**
 设置Sentinel数量
 @param i 数
 */
- (void)setSentinel:(NSInteger)i;

/**
 获取Sentinel数量
 @return Sentinel数量
 */
- (NSInteger)getSentinel;

/**
 如果当前展示了HUD 设置为YES 反之NO
 @param show BOOL
 */
- (void)setShowHUD:(BOOL)show;

/**
 当前是否展示HUD
 @return YES正在展示,反之不在展示
 */
- (BOOL)getShowHUD;
@end

#import "IdiotSentinelProgressHUD.h"
#import <objc/runtime.h>
@implementation IdiotSentinelProgressHUD
+ (void)showHUDAddedTo:(UIView *)view animated:(BOOL)animated {
    [IdiotSentinelProgressHUD showHUDAddedTo:view withCount:1 animated:animated];
}
+ (void)showHUDAddedTo:(UIView *)view withCount:(NSInteger)count animated:(BOOL)animated {
    NSInteger i = [view getSentinel] + count;
    [view setSentinel:i];
    if ([view getShowHUD]) {
        return;
    }
    if (i > 0) {
        [MBProgressHUD showHUDAddedTo:view animated:animated];
        [view setShowHUD:YES];
    }
}
+ (void)hideHUDForView:(UIView *)view animated:(BOOL)animated endBlock:(void (^)(NSInteger i))end {
    NSInteger i = [view getSentinel] - 1;
    [view setSentinel:MAX(i,0)];
    i = [view getSentinel];
    if (i <= 0) {
        [MBProgressHUD hideHUDForView:view animated:animated];
        [view setShowHUD:NO];
        if (end) {
            end(i);
        }
        return;
    }
    if (end) {
        end(i);
    }
}
+ (void)finalHideHUDForView:(UIView *)view animated:(BOOL)animated endBlock:(void (^)(NSInteger i))end {
    [MBProgressHUD hideHUDForView:view animated:animated];
    [view setSentinel:0];
    [view setShowHUD:NO];
    if (end) {
        end(0);
    }
}
@end

static NSString *SentinelCount = @"SentinelCount";
static NSString *SentinelShowHUD = @"SentinelShowHUD";
@implementation UIView (Sentinel)
- (void)setSentinel:(NSInteger)i {
    @synchronized(self) {
        objc_setAssociatedObject(self, &SentinelCount, [NSNumber numberWithInteger:i], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
}
- (NSInteger)getSentinel {
    @synchronized(self) {
        NSNumber *i = objc_getAssociatedObject(self, &SentinelCount);
        return i==nil?0:[i integerValue];
    }
}
- (void)setShowHUD:(BOOL)show {
    @synchronized(self) {
        objc_setAssociatedObject(self, &SentinelShowHUD, [NSNumber numberWithBool:show], OBJC_ASSOCIATION_ASSIGN);
    }
}
- (BOOL)getShowHUD {
    @synchronized(self) {
        NSNumber *show = objc_getAssociatedObject(self, &SentinelShowHUD);
        return show==nil?NO:[show boolValue];
    }
}
@end

使用效果:


gif5新文件.gif
上一篇 下一篇

猜你喜欢

热点阅读