2

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

Hashem Aboonajmi
  • 13,077
  • 8
  • 66
  • 75
  • Why do you need an entire view controller just to show an activity indicator? Why don't you just pop the activity indicator on the first view? – nhgrif Dec 05 '13 at 20:27
  • second view controller does a big job. it create content for pages. it truncates text and add them to an array which will be used for pages. it prepares the before and after pages.if your search for implementing UIPageViewController you'll see it will implemented in this pattern – Hashem Aboonajmi Dec 05 '13 at 20:48
  • While it may do a lot of behind the scenes work, it doesn't seem to do much on the UI end, which I why I question its necessity. You can put the work of the second view controller in another class, and instantiate an object of that class in the first view if you want to split it into different files... but I just don't even remotely understand why the actual separate view controller is necessary. – nhgrif Dec 05 '13 at 20:52
  • 1
    Just seems like an unnecessary complication to have the middle view controller that, from a UI standpoint, does nothing. – nhgrif Dec 05 '13 at 20:53
  • I have added the truncate method to another thread when the text for the first page prepared immediately shows the page. – Hashem Aboonajmi Dec 05 '13 at 20:55
  • But again, from the UI point of view, or the "MVC" point of view, the middle view controller is entirely pointless. ALL the work that it is doing can be done on the first view controller (with an activity indicator popping up), and then you just segue into the 3rd view controller from the 1st and get rid of the 2nd one completely. – nhgrif Dec 05 '13 at 20:57
  • I have updated the question. Added more information maybe usefull. You can think about second viewController as a seperqted class. I'm instantiating secondVC from firstVC. – Hashem Aboonajmi Dec 05 '13 at 21:13
  • @nhgrif I have uploaded sample app please see it. – Hashem Aboonajmi Dec 06 '13 at 17:02

2 Answers2

1

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.

Darren
  • 10,182
  • 20
  • 95
  • 162
1

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];
Hashem Aboonajmi
  • 13,077
  • 8
  • 66
  • 75