Search code examples
iosios5asihttprequestmbprogresshud

ASINetworkQueue and MBProgressHUD


I use a ASINetWorkQueue in a ViewController. So, during the queue is performing, i want to show a MBProgressHUD.

- (void) addItemsToEndOfTableView{
NSLog(@"add items");
[[self networkQueue] cancelAllOperations];

// Création d'une nouvelle file (queue) de requetes
[self setNetworkQueue:[ASINetworkQueue queue]];
[[self networkQueue] setDelegate:self];
[[self networkQueue] setRequestDidFinishSelector:@selector(requestFinished:)];
[[self networkQueue] setRequestDidFailSelector:@selector(requestFailed:)];
[[self networkQueue] setQueueDidFinishSelector:@selector(queueFinished:)];

...add requests

HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.dimBackground = YES;
HUD.delegate = self;

[HUD showWhileExecuting:@selector(stopHub) onTarget:self withObject:nil animated:YES];
}
[[self networkQueue] go];

so, when queueFinished is call, i want to stop the hud:

- (void)queueFinished:(ASINetworkQueue *)queue
{
    [self stophud];
}

-(void)stophud
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

but actually, the progress Hud disappear quickly, whereas activity indicator in the top bar of iphone is running while data being collect.

So, what's wrong ?


Solution

  • From MBprogressHUD API

    /** 
     * Shows the HUD while a background task is executing in a new thread, then hides the HUD.
     *
     * This method also takes care of autorelease pools so your method does not have to be concerned with setting up a
     * pool.
     *
     * @param method The method to be executed while the HUD is shown. This method will be executed in a new thread.
     * @param target The object that the target method belongs to.
     * @param object An optional object to be passed to the method.
     * @param animated If set to YES the HUD will (dis)appear using the current animationType. If set to NO the HUD will not use
     * animations while (dis)appearing.
     */
    - (void)showWhileExecuting:(SEL)method onTarget:(id)target withObject:(id)object animated:(BOOL)animated;
    

    Since you are using this method, your stophud is executed in a new thread. This could cause the strange problem you have (I suppose).

    Insetad of using it, try to use

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    // set the properties here...
    

    to show it after starting the queue and

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

    to hide it when the queue has finished.

    Hope it helps.