有時候我們會遇到在同一個頁面發(fā)起多個網(wǎng)絡(luò)請求的情況忠烛。普遍的做法是采用dispatch_group,當(dāng)組內(nèi)的所有方法都完成之后執(zhí)行reloadData,達(dá)到只顯示一次加載動畫的效果耘分。
先來看看代碼:
- (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(), ^{
//所有請求完成,結(jié)束動畫
[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);
}];
}
這種做法绑警,在網(wǎng)絡(luò)請求不是很多求泰,并且網(wǎng)速良好的情況,用戶體驗很好计盒,但是當(dāng)網(wǎng)絡(luò)請求很多渴频,同時網(wǎng)絡(luò)差的時候,用戶會一直看到MBProgressHUD的等待動畫北启。我們希望卜朗,在加載完一個請求之后,展示UI暖庄,同時MBProgressHUD的等待動畫繼續(xù)聊替,以此類推,當(dāng)所有的請求完成之后培廓,結(jié)束MBProgressHUD等待動畫惹悄。
下面是代碼演示,代碼很粗糙肩钠,大家可以根據(jù)思想自己去實現(xiàn)泣港。
#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)
/**
設(shè)置Sentinel數(shù)量
@param i 數(shù)
*/
- (void)setSentinel:(NSInteger)i;
/**
獲取Sentinel數(shù)量
@return Sentinel數(shù)量
*/
- (NSInteger)getSentinel;
/**
如果當(dāng)前展示了HUD 設(shè)置為YES 反之NO
@param show BOOL
*/
- (void)setShowHUD:(BOOL)show;
/**
當(dāng)前是否展示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