Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlert takes a few seconds to show up

I'm having a problem where all the UIAlertViews in my app take quite some time to show up. The display dims instantly but the actual alert needs like 5 seconds to be displayed.

I'm creating them like this:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"
                                                    message:@"Message"
                                                   delegate:nil 
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];
[alertView show];
[alertView release];

Anyone ever had this?

Thanks
–f

like image 577
flohei Avatar asked Dec 09 '11 10:12

flohei


2 Answers

If you try to show UIAlertView NOT from main thread, you can see this type of delay (and, sometimes, more serious bugs and crashes).

Extract code as a separate method and call it using "performSelectorOnMainThread", or use GCD and dispatch it there.

like image 54
bealex Avatar answered Oct 20 '22 03:10

bealex


What are you doing after that code? If you do some computations and go on to work on things, the alert would not display. It is only displayed at the end of the runloop. The best way to handle this is to split up the work into smaller chunks, doing one after the other, while letting the runloop process events in between. Or offloading the heavy lifting to a background thread.

If you just what to try, if this is indeed the problem you are experiencing, try to add

 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];

just below your code above.

like image 23
tonklon Avatar answered Oct 20 '22 03:10

tonklon