2

I am developing an iOS 5.1 application on Xcode 4.2. I have a uitablcontroller with different tabs. My problem is when a tab is clicked , the application 'freezes' for few seconds and does all the codes it's meant to do, but it does not load the UIAlertView first as it should be.

I have the UIAlertView declared in the viewDidLoad. Here is a code snippet:

- (void)viewDidLoad
{



NSLog(@"##### VIEW DID LOAD 1 #####");


// Display Alert: Loading

alertView = [[UIAlertView alloc] initWithTitle:@"Loading"
                                       message:@"\n"
                                      delegate:self
                             cancelButtonTitle:nil
                             otherButtonTitles:nil];

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur
[alertView addSubview:spinner];
[spinner startAnimating];
[alertView show];

[super viewDidLoad];


NSLog(@"##### VIEW DID LOAD 2 #####");


self.tableView.dataSource = self;
self.tableView.delegate = self;

[self callMainMethod];

}

When the tab is clicked, I can see that the first NSLog's are displayed in the Log, and then the main method is called, but the UIAlertview is not displayed.

mhmdshawqi
  • 349
  • 3
  • 9
  • Errata, I am using Xcode 4.5 – mhmdshawqi Dec 03 '12 at 14:57
  • Try calling the superview's `viewDidLoad` before your custom implementation. – Abizern Dec 03 '12 at 16:08
  • ## Solution ## emrys57's answer solved the problem! The answer is to move the [self callMainMethod] to viewDidAppear. I had also needed to put [self.tableview reloadData] after calling the function as I needed to view the data on a Table. – mhmdshawqi Dec 03 '12 at 18:02

1 Answers1

1

When viewDidLoad is running, it may be that the frame of the associated UIView has zero size. I don't know, but the UIAlertView may be trying to present itself in this zero-size frame. Does it make sense if you present the UIAlertView in viewDidAppear?

If [self callMainMethod] is taking lots of compute power, then the display might not be updated until it finishes. You could try moving it to viewDidAppear. You could also try delaying it, so that the main run loop for the UI thread, the thread that the display is updated on and the thread that executesviewDidLoad and all the other view... methods, has time to complete everything and become idle before you start the heavy processing. It's only when the run loop has done all the processing it can that it starts actually to update the display. Like this:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 200000000), dispatch_get_main_queue(), ^{
   [self callMainMethod];
}

The documentation for that is at http://developer.apple.com/library/ios/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html

If that suspicion is correct, you should then get the UIAlertView popping straight up, only to have everything go dead for a while, while callMainMethod executes.

I wish I knew how to write, "dispatch this block only after you've managed to finish updating the display with everything up to here", but I don't know how to do that. So the dispatch call above should delay the call to callMainMethod by 200ms, which is usually plenty.

If that works, you should probably start another question, something like "How can I stop the display freezing while I execute this method."

emrys57
  • 6,679
  • 3
  • 39
  • 49
  • I tried adding the UIAlertView in the viewWillAppear as well as viewDidAppear, and it still 'freezes' when the tab is clicked, then displays the data and the alert. Thanks for your face response. I appreciate your help. =) – mhmdshawqi Dec 03 '12 at 14:56
  • That's progress, at least. I updated the answer, and you could try that. – emrys57 Dec 03 '12 at 16:05
  • That was it!! Thanks alot! Moving the mainMethod to the ViewDidAppear did solve the problem. Thanks alot for the very detailed and explanatory answer. – mhmdshawqi Dec 03 '12 at 17:58
  • Jolly good! Pleased to be able to help. And thank you very much for the tick. – emrys57 Dec 03 '12 at 18:23