Baorong Li
Baorong Li

Reputation: 48

MBProgressHUD hide before tableView reloadData()

Here are the codes,HUD just hide very quickly. And I need 2~5s to reloadData,is there any way to accelerate or just keep the HUD appear?

func loadDorms() {

    var hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
    hud.labelText = "加载中"

    hud.showAnimated(true, whileExecutingBlock:{
        let manager = AFHTTPRequestOperationManager()
        manager.GET(ServiceAPi.getRegionList(23),
            parameters: nil,
            success: {
                operation,responseObject in

                if let quote = responseObject as? NSArray {
                    self.dorms = quote
                    self.dorm  = quote[0] as! NSDictionary
                } else {
                    print("no quote")
                }
                self.leftTableView.reloadData()
                self.rightTableView.reloadData()

            }, failure: {
                operation, error in

                print("Error: " + error.localizedDescription)
        })

        }){
            // 移除
            hud.removeFromSuperview()
            hud = nil
    }
}

Upvotes: 0

Views: 123

Answers (3)

Rumin
Rumin

Reputation: 3790

You are using asynchronous call. Thus, the UI changes will take place immediately before the completion of success block. Thus, if you want your MBProgressHUD to hide after table reload, hide it in the success block.

      success: {
                operation,responseObject in

                if let quote = responseObject as? NSArray {
                    self.dorms = quote
                    self.dorm  = quote[0] as! NSDictionary
                } else {
                    print("no quote")
                }
                self.leftTableView.reloadData()
                self.rightTableView.reloadData()

               // 移除
               hud.removeFromSuperview()
               hud = nil
             }, failure: {

               // 移除
               hud.removeFromSuperview()
               hud = nil
               operation, error in

               print("Error: " + error.localizedDescription)
    }

Upvotes: 1

user3432164
user3432164

Reputation:

MBProgressHUD has functionality to execute for custom time span.

See below code.

[MBProgressHUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];

Create method where you can set for how much time you want to show MBProgressHUD.

- (void)myTask
{

    sleep(3);//set the time accordingly your need.

}

May be it will help you.

Upvotes: 0

Abhinav
Abhinav

Reputation: 38152

Move hud.removeFromSuperview() right before reloadData() call inside the block. Problem is your block is executing little later than other sequential code.

And I need 2~5s to reloadData

If you mean that reloadData() takes 2-5 seconds then you might want to cell your cellForRowAtIndexPath function. reloadData() is almost instantaneous, given you are not doing heavy calculations in any of the datasource methods especially in cellForRowAtIndexPath.

As a side note, it is advisable to access class properties inside the block with a weak reference to self. Something like this:

weak var aBlockSelf = self
aBlockSelf.leftTableView.reloadData()
aBlockSelf.rightTableView.reloadData()

Upvotes: 0

Related Questions