添加hud容错处理

This commit is contained in:
2026-02-24 16:23:57 +08:00
parent bc623676ca
commit 27d4b2b817
2 changed files with 31 additions and 2 deletions

View File

@@ -17,6 +17,7 @@ static __weak MBProgressHUD *sHUD = nil;
static KBHUDMaskType sMaskType = KBHUDMaskTypeClear; // static KBHUDMaskType sMaskType = KBHUDMaskTypeClear; //
static BOOL sDefaultTapToDismiss = NO; // static BOOL sDefaultTapToDismiss = NO; //
static NSTimeInterval sAutoDismiss = 1.2; static NSTimeInterval sAutoDismiss = 1.2;
static NSTimeInterval sLoadingMaxDuration = 20.0; // loading
static __weak UIView *sContainerView = nil; // static __weak UIView *sContainerView = nil; //
#pragma mark - Private Helpers #pragma mark - Private Helpers
@@ -129,8 +130,27 @@ static __weak UIView *sContainerView = nil; // 缺省承载视图(
[self dismiss]; [self dismiss];
} }
+ (void)_kb_loadingTimeoutDismiss {
[self dismiss];
}
+ (void)cancelLoadingAutoDismiss {
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(_kb_loadingTimeoutDismiss)
object:nil];
}
+ (void)scheduleLoadingAutoDismiss {
[self cancelLoadingAutoDismiss];
if (sLoadingMaxDuration <= 0) { return; }
[self performSelector:@selector(_kb_loadingTimeoutDismiss)
withObject:nil
afterDelay:sLoadingMaxDuration];
}
+ (void)_showText:(NSString *)text icon:(nullable UIImage *)icon { + (void)_showText:(NSString *)text icon:(nullable UIImage *)icon {
[self onMain:^{ [self onMain:^{
[self cancelLoadingAutoDismiss];
MBProgressHUD *loadingHUD = sHUD; MBProgressHUD *loadingHUD = sHUD;
if (loadingHUD) { if (loadingHUD) {
[loadingHUD hideAnimated:YES]; [loadingHUD hideAnimated:YES];
@@ -177,6 +197,7 @@ static __weak UIView *sContainerView = nil; // 缺省承载视图(
if (!hud) { return; } if (!hud) { return; }
hud.mode = MBProgressHUDModeIndeterminate; hud.mode = MBProgressHUDModeIndeterminate;
hud.label.text = status ?: @""; hud.label.text = status ?: @"";
[self scheduleLoadingAutoDismiss];
}]; }];
} }
@@ -191,6 +212,7 @@ static __weak UIView *sContainerView = nil; // 缺省承载视图(
hud.mode = MBProgressHUDModeDeterminate; hud.mode = MBProgressHUDModeDeterminate;
hud.progress = progress; hud.progress = progress;
hud.label.text = status ?: @""; hud.label.text = status ?: @"";
[self scheduleLoadingAutoDismiss];
}]; }];
} }
@@ -205,9 +227,9 @@ static __weak UIView *sContainerView = nil; // 缺省承载视图(
+ (void)showError:(NSString *)status { [self _showText:status ?: KBLocalized(@"Failed") icon:nil]; } + (void)showError:(NSString *)status { [self _showText:status ?: KBLocalized(@"Failed") icon:nil]; }
+ (void)dismiss { [self onMain:^{ [sHUD hideAnimated:YES]; }]; } + (void)dismiss { [self onMain:^{ [self cancelLoadingAutoDismiss]; [sHUD hideAnimated:YES]; }]; }
+ (void)dismissWithDelay:(NSTimeInterval)delay { [self onMain:^{ [sHUD hideAnimated:YES afterDelay:delay]; }]; } + (void)dismissWithDelay:(NSTimeInterval)delay { [self onMain:^{ [self cancelLoadingAutoDismiss]; [sHUD hideAnimated:YES afterDelay:delay]; }]; }
#pragma mark - Config #pragma mark - Config

View File

@@ -387,6 +387,10 @@ autoShowBusinessError:YES
self.manager.responseSerializer = [AFHTTPResponseSerializer serializer]; self.manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSURLSessionDataTask *task = [self.manager dataTaskWithRequest:req uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { NSURLSessionDataTask *task = [self.manager dataTaskWithRequest:req uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
NSLog(@"[KBNetworkManager] task finished, error = %@", error); NSLog(@"[KBNetworkManager] task finished, error = %@", error);
// / loading HUD
dispatch_async(dispatch_get_main_queue(), ^{
[KBHUD dismiss];
});
// AFN 2xx error // AFN 2xx error
if (error) { if (error) {
#if DEBUG #if DEBUG
@@ -494,6 +498,9 @@ autoShowBusinessError:YES
self.manager.responseSerializer = [AFHTTPResponseSerializer serializer]; self.manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSURLSessionDataTask *task = [self.manager dataTaskWithRequest:req uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { NSURLSessionDataTask *task = [self.manager dataTaskWithRequest:req uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
NSLog(@"[KBNetworkManager] data task finished, error = %@", error); NSLog(@"[KBNetworkManager] data task finished, error = %@", error);
dispatch_async(dispatch_get_main_queue(), ^{
[KBHUD dismiss];
});
if (error) { if (error) {
if (completion) completion(nil, response, error); if (completion) completion(nil, response, error);
return; return;