Why does creating a UIAlertView instance take so long?

I have the code for the iOS application that I am developing, and for some reason, what seems like a quick and easy task makes my iPhone 4S execute a full second or more each time.

The context is ... I have an ActionSheet popup with 2 buttons, and if the user clicks one of the buttons, the application seems to stop for about a second. Here is the code:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) { UIAlertView* newTimerAlertView = [[[UIAlertView alloc] initWithTitle:@"Create New Timer" message:@"Enter a name for your new indicator" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Create", nil] autorelease]; newTimerAlertView.tag = kNewTimer; newTimerAlertView.alertViewStyle = UIAlertViewStylePlainTextInput; [newTimerAlertView show]; } else if (buttonIndex == 1) { NSLog(@"ActionSheet button 2 tapped"); UIAlertView* newTallyAlertView = [[[UIAlertView alloc] initWithTitle:@"Create New Tally" message:@"Enter a name for your new indicator" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Create", nil] autorelease]; newTallyAlertView.tag = kNewTally; newTallyAlertView.alertViewStyle = UIAlertViewStylePlainTextInput; [newTallyAlertView show]; NSLog(@"end"); } } 

Focusing on the second button for simplicity (although the first button behaves the same), the log looks like this:

 2012-01-25 20:35:46.330 ...[177:707] ActionSheet button 2 tapped 2012-01-25 20:35:47.194 ...[177:707] end 2012-01-25 20:35:56.154 ...[177:707] ActionSheet button 2 tapped 2012-01-25 20:35:56.180 ...[177:707] end 

Please note that the first time I tried it, it takes a second before the code fragment completes execution, but the second time (and all subsequent times) the code takes only about 30 or less milliseconds.

Is there something wrong with the code? or do I just need to compensate for the delay in viewing progress?

Thanks!

EDIT: This only happens on devices when starting the application from Xcode ... maybe this is due to the debugger?

+4
source share
2 answers

Now I, if the warning is not in the main thread, they can be delayed

+3
source

Xcode does not slow down the display of warnings in any situation that I have encountered.

Your code looks pretty simple vanilla, so I suspect something is happening outside of the published snippet.

I see that you are referring to timers ... maybe this is a manifestation of something else, like a timer callback going to the main thread? Try using the Time Profiler or System Trace tools to find out what kind of lock (something, of course, is).

0
source

Source: https://habr.com/ru/post/1393080/


All Articles