Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using UIAlertView while waiting for calculations/processing data

I've set up my iphone application in a tab layout, and I would like to perform some rather intense calculations (can take several seconds to get a result) when the user selects one of the tabs.

Originally, it would appear the iphone would just hang on the original tab while doing the number crunching.

I tried adding an UIAlertView as some eye-candy, but I'm getting a fade to grey for a few seconds, then after the computations are done, a quick appearance/disappearance of the View. What I want to see is the UIAlertView appear/animate when the user touches the tab, and then disappear once the calculations are done

- (void)viewDidAppear:(BOOL)animated
{    


    UIAlertView *baseAlert = [[[UIAlertView alloc] initWithTitle:@"Calculating" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil]autorelease];
    [baseAlert show];
    UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    aiv.center = CGPointMake(baseAlert.bounds.size.width /2.0f, baseAlert.bounds.size.height - 40.0f);
    [aiv startAnimating];
    [baseAlert addSubview:aiv];
    [aiv release];


/*** calculation and display routines***/


    [baseAlert dismissWithClickedButtonIndex:0 animated:YES];
}

I've already seen this post, but I can't seem to figure out how apply it to my case.

like image 900
Rasman Avatar asked Jul 27 '26 06:07

Rasman


1 Answers

The easiest way to solve this is with blocks; First schedule calculations to separate thread using first block and when done dismiss alert view via block dispatched on main thread:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL), ^{

// Do calculations


dispatch_async(dispatch_get_main_queue(), ^
                       {
                          [baseAlert dismissWithClickedButtonIndex:0 animated:YES];
                       });
});
like image 95
TheBlack Avatar answered Jul 28 '26 21:07

TheBlack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!