문제

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

도움이 되었습니까?

해결책

Shortly you can do it like this:

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

Check MBProgressHUD's usage

다른 팁

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..

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top