Joson Daniel
Joson Daniel

Reputation: 409

How to add MBProgressHUD waiting loading data in iOS

I have a problem: I used AFNetworking to get data from server, i used NSOperationQueue to add many operation to it, in each request, I added this operation to queue and used waitUntilAllOperationsAreFinished as bellow :

request 1
...
    [queue addOperation:operation1];
    [queue waitUntilAllOperationsAreFinished];

request 2
...
    [queue addOperation:operation2];
    [queue waitUntilAllOperationsAreFinished];

I tried above code and my programs seems hangs and after that, it runs ok.So that I want to added MBProgressHUD to waiting queue finish, then I want to check if queue finish, I want to hide MBProgressHUD. But when i click on Button to load UIViewController, MBProgressHUD cannot show.

HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = @"Loading";

Actually, I want to show MBProgressHUD when queue finish. How can i do that? Thanks all

Upvotes: 2

Views: 15385

Answers (3)

Zar E Ahmer
Zar E Ahmer

Reputation: 34360

Another Better Approach..

  HUD = [[MBProgressHUD alloc] initWithView:self.view];
    HUD.labelText = @"Doing funky stuff...";
    HUD.detailsLabelText = @"Just relax";
    HUD.mode = MBProgressHUDModeAnnularDeterminate;
    [self.view addSubview:HUD];

    [HUD showWhileExecuting:@selector(doSomeFunkyStuff) onTarget:self withObject:nil animated:YES];

And doSomeFunkyStuff

- (void)doSomeFunkyStuff {
    float progress = 0.0;

    while (progress < 1.0) {
        progress += 0.01;
        HUD.progress = progress;
        usleep(50000);
    }
}

Detail answer is here..

Upvotes: 3

Mike Pollard
Mike Pollard

Reputation: 10195

waitUntilAllOperationsAreFinished will bock the current thread, which is probably the main thread so you really don't want to do that.

If you're using AFNetworking then check out this method on AFHTTPClient

- (void)enqueueBatchOfHTTPRequestOperations:(NSArray *)operations 
                              progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock 
                            completionBlock:(void (^)(NSArray *operations))completionBlock;

So show you HUD then call this method and hide your HUD in the completionBlock

Upvotes: 2

sunkehappy
sunkehappy

Reputation: 9091

Shortly you can do it like this:

[MBProgressHUD showHUDAddedTo:self.view animated:YES];
[MBProgressHUD hideHUDForView:self.view animated:YES];

Check MBProgressHUD's usage

Upvotes: 12

Related Questions