MBProgressHUD needs to be accessed on the main thread.報錯的解決辦法

MBProgressHUD和SVP是最常見的狀態顯示的第三方庫了,最近遇到個MB的問題,如標題所示,崩潰崩到三方庫的內部了,字面意思是必須要放到主線程了,請看我最初寫的代碼:

-(void)showMessage:(NSString *)message{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
      hud.mode = MBProgressHUDModeText;
    hud.label.text = message;
    hud.bezelView.backgroundColor = [UIColor blackColor];
        hud.label.textColor = [UIColor whiteColor];
    hud.offset = CGPointMake(0.f, 60);
     [hud hideAnimated:YES afterDelay:2.f];

}

沒有放到主線程裏邊,正常情況也沒關心,後來在Block內部使用,直接崩潰,不知道是不是又特殊的原因,於是按照給的錯誤報告,改之後的代碼爲:

 +(void)showMessage:(NSString *)message{
 
               dispatch_async(dispatch_get_main_queue(), ^{
                   FalconProgressHUD *hud = [FalconProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
                   hud.mode = FalconProgressHUDModeText;
                   hud.label.text = message;
                   hud.bezelView.backgroundColor = [UIColor blackColor];
                   hud.label.textColor = [UIColor whiteColor];
                   hud.offset = CGPointMake(0.f, 60);
                   [hud hideAnimated:YES afterDelay:2.f];
          });

    
}


網上有其他的資料寫法是先放到全局隊列,再放到主隊列,這樣應該也是可以的,如上即可解決崩潰的問題



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章