UIAlertAction handler running after delay
Asked Answered
C

3

17

I'm trying to change my UIAlertViews to UIAlertControllers. I set up this action for it:

UIAlertAction *undoStopAction = [UIAlertAction actionWithTitle:@"Undo Stop"
                                                        style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction *action) {
                                                           [self undoStop];
                                                       }];

But, the handler doesn't run until about a second after the action is tapped. Is there any way to speed this up?

Chairman answered 7/9, 2015 at 15:18 Comment(7)
Are you doing some stuff on main thread?Bant
Or are you running this inside a background method/blockIssy
if you are not displaying the alert using main thread, then do it in main thread.Watkins
Everything is from the main thread.Chairman
Show the complete method for undoStopIssy
It's not about undoStop, because I've replaced that line with a single NSLog, and the delay is the same. Even when calling this from the main controller's viewDidLoad, it still happens. It seems the handler for UIAlertAction is just always delayed.Chairman
@PeterCarnesciali I have the same issue...it'd be great to get a real resolution on this! None of the comments or answers are successful in addressing the issue. Have you found a solution? (I don't want to use the deprecated UIAlertView)Rectitude
D
3

The short delay is normal for Alert View (less than a second though). If this is not convenient for you, you can programmatically create a view that covers the screen with a label and a button, basically a customized alert view.

Delfinadelfine answered 7/9, 2015 at 19:56 Comment(1)
Thanks. I previously had it as a UIAlertView, with a specific behavior when the art button was tapped. That has always been instant. I guess I'll keep doing that for now, even though it's deprecated.Chairman
R
1

There is a semi-solution here:

The handler of a UIAlertAction is a bit too late - how can I make it immediate?

Basically, subclass the alert controller and run a handler on viewWillDisappear. You can store the handler block on your alert controller.

The problem with this solution is that you can't run a handler per button like you can if you add action(s) with UIAlertAction. So that's a major limitation.

However, this solution worked for me (because I was trying to add a background view behind the alert when the alert appeared and make it animate away with the alert when the alert disappeared...originally I tried to do it with the button tap, but that didn't work because of the handler being delayed).

Rectitude answered 11/7, 2017 at 19:25 Comment(0)
L
0

As implied by the comment left by Mr. T, it's possible that the handler is not being called on the main thread, which could explain the delay. Try embedding your handler code in :

dispatch_async(dispatch_get_main_queue()) {
<your code>
}

and see if you still get the delay.

Lisabeth answered 14/12, 2015 at 17:11 Comment(1)
No change. Seems like the handler is not executed until after a second. Unlike how it was when I used UIAlertViewChairman

© 2022 - 2024 — McMap. All rights reserved.