MBProgressHUD doesn't show when using it in tableView

927 views Asked by At

I'v implemented tableView with searchBar (used searchDispalyController) which user can enter a search term and table view shows which stories contains this search term . after tapping on the cell, a pageBasedView will push to the view which contains text and user can curl pages. (the app is a book).

the heavy task is on a method that truncate the a huge text into small parts and add them to an array.

so when user taps on a cell the burden of truncating is transferred to pageBasedViewController. it takes about a few seconds to truncating be completed and pages be ready for navigating.

in these few seconds I want to show an activityIndicator which I used MBProgressHud.

the problem rise here when tapping on a cell, the activityIndicator won't appear until the new page is going to the view.

 [MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
//the heavy task
    [self truncate:contentString];
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    });
});

here is an screenShot which shows the flow of app: enter image description here

Im amazed why I can't see the ActivityIndicator! after removing this code:

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

swiping back to masterViewController and again tapping on the cell process take a few second and surprisingly will see the indicator is working! but Im amazed why for the first time it doesn't work?

from noon until midnight Im working on it. but no solution!

I have uploaded sample app download from here: (67 KB)

http://www.mediafire.com/download/decvu7uig9r6v1a/pageBasedApp.zip

2

There are 2 answers

0
Hashem Aboonajmi On BEST ANSWER

solved. I did this way:

[MBProgressHUD showHUDAddedTo:[[[UIApplication sharedApplication] windows] objectAtIndex:1] animated:YES];
sema = dispatch_semaphore_create(0);
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantPast]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
//here we do the heavy job
[self truncate:contentString];
});

and after recieving semaphore: . . .

dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

    NSLog(@"<<< signal received >>>");
    [MBProgressHUD hideHUDForView:[[[UIApplication sharedApplication] windows] objectAtIndex:1] animated:YES];
1
Darren On

Your code is showing the HUD then removing it immediately.

You show the HUD, then set up a task to run on a background thread, then remove the HUD before the task has completed.

I'd suggest adding a completion block to your method so you know when it's been completed and then remove the HUD.

An even better thing to do is to put your method in the first controller and call it whilst showing the HUD, then push to the final controller. No need for a middle man.