Using UIAlertView while waiting for calculations/processing data

拈花ヽ惹草 提交于 2020-01-05 02:52:29

问题


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.


回答1:


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];
                       });
});



回答2:


You need to understand how the event loop works. When you call [baseAlert show], the alert view is added to the view hierarchy, but it isn't actually drawn to the screen until the current code block ends and control returns to the event loop. By doing your computation immediately after asking the alert view to show, you are preventing the alert from ever appearing.

It's kind of like writing a letter telling somebody you plan to paint your house, spending a week painting your house, then writing another letter saying you've done it, and THEN taking both letters and dropping them in the mailbox to be delivered at the same time.

If you have an expensive computation, the easiest way to handle it in iOS 4 and later is to place a block of code in a dispatch queue, so the work will be done in a background thread, and the main thread can still update the screen and respond to finger taps.

 [baseAlert show];
 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL), ^{
     // we're on a secondary thread, do expensive computation here
     // when we're done, we schedule another block to run on the main thread
     dispatch_async(dispatch_get_main_queue(), ^{
         // this code is back on the main thread, where it's safe to mess with the GUI
         [baseAlert dismissWithClickedButtonIndex:0 animated:YES];
     });
 });



回答3:


What is probably happening is that the "intense calculations" you're doing are being run in the same thread as where you're calling the UIAlertView. Setting a delegate for UIAlertView would set that up in a separate thread so that you don't need to worry about contention and whether the UIAlertView will show up before the calculations.

Alternatively, using a UIAlertView is a rather heavy handed approach - perhaps you could use some other interface element to indicate progress instead of rendering the app useless while you crunch some numbers?



来源:https://stackoverflow.com/questions/6170333/using-uialertview-while-waiting-for-calculations-processing-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!