如何在服务器未返回数据时停止MBProgressHUD并添加子视图

mat*_*mat 8 api json ios swift

在我的应用程序中,我有这个类从我的服务器获取数据:

class Api{

func loadOffers(completion:(([Offers])-> Void), offer_id: String, offerStatus:String){


    let myUrl = NSURL(string: "http://www.myServer.php/api/v1.0/offers.php")
    let request = NSMutableURLRequest(URL: myUrl!)
    request.HTTPMethod = "POST"
    let postString = "offer_id=\(offer_id)&offerStatus=\(dealStatus)&action=show"
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
        { data, response, error in              
            if error != nil {                  
                println("error\(error)")                 
            }else{   
               var err:NSError?

               let jsonObject : AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil)

                if let dict = jsonObject as? [String: AnyObject] {

                if let myOffers = dict["offers"] as? [AnyObject] {

                var offers = [Offers]()

                for offer in myOffers{

                let offer = Offers(dictionary: offer as! NSDictionary)

                offers.append(offer)                       

                let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
                                    dispatch_async(dispatch_get_global_queue(priority, 0 )){
                                                                                   dispatch_async(dispatch_get_main_queue()){

                completion(offers)

                             }         
                           }                                
                        }
                    }
                }                
            }            
      }
    task.resume()       
   }
 }
Run Code Online (Sandbox Code Playgroud)

然后在我的View Controller中加载模型:

    class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {


    var offers: [Offers]!

    func loadModel() {
    let loadingNotification = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
    loadingNotification.mode = MBProgressHUDMode.Indeterminate
    loadingNotification.labelText = "updating your offers..."
    offers = [Offers]()
    let api = Api()
    api.loadOffers(didLoadOffers , offer_id: dealIdent!, offerStatus: "open")

    }

    func didLoadOffers(offers:[Offers]){

    self.offers = offers
    self.tableView.reloadData()
    MBProgressHUD.hideAllHUDsForView(self.view, animated: true)
    self.refreshControl.endRefreshing()
    }

    override func viewWillAppear(animated: Bool) {

    loadModel()

    }
 }
Run Code Online (Sandbox Code Playgroud)

一切都有效,除了当JSON字典为空时,意味着没有提供MBProgressHUD保持旋转.在此输入图像描述

我想停止活动指示器添加一个子视图,而不是说没有优惠.任何建议将不胜感激.

我试过了:

if offers.isEmpty{ MBProgressHUD.hideAllHUDsForView(self.view, animated: true) }

并且

if offers == 0 { MBProgressHUD.hideAllHUDsForView(self.view, animated: true) }

但它不起作用

谢谢

Pen*_*esh 3

发生这种情况是因为您在主队列中设置了 HUD,但试图从另一个队列中删除。所有与 UI 相关的更改都应在main_queue()

尝试使用此代码

dispatch_async(dispatch_get_main_queue(), { 

  // your code to modify HUD here

});
Run Code Online (Sandbox Code Playgroud)