Artur Gurgul
Artur Gurgul

Reputation: 396

Why does my UIAlertView only appear after a delay?

In example code below UIAlertView is show after delay, but i need display it immediately

//metoda zapisuje komentrz na serwerze
-(void) saveAction {

    UIAlertView *progressAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"imageGalleries.sendAction", @"") message:@" " delegate:self cancelButtonTitle:NSLocalizedString(@"alert.cancel", @"") otherButtonTitles:nil];

    [progressAlert addSubview:progressView];
    [progressAlert show];

    // some long performance instructions
}

- (void)loadView {
    [super loadView];
    self.navigationItem.rightBarButtonItem =  [NavButton buttonWithTitle:NSLocalizedString(@"sendImage.saveButtonTitle", @"") target:self action:@selector(saveAction)];
    progressView = [[UIProgressView alloc] initWithFrame: CGRectMake(30.0f, 80.0f - 26, 225.0f, 10.0f)];
}

Why UIAlertView do not show immediately when I call saveAction?

Upvotes: 2

Views: 2930

Answers (4)

Quanlong
Quanlong

Reputation: 25476

If you show the alert in background thread, it will also have a delay.

Upvotes: 0

Zepplock
Zepplock

Reputation: 29135

Have you considered using MBProgressHUD for long lasting operations? It'll wrap those calls into separate threads for you and is very flexible from functionality and UI perspectives

Upvotes: 1

zoul
zoul

Reputation: 104065

If the “long performance instructions” following the alert code run on the main thread, they will block the alert from appearing. Read something about Cocoa run loops, that should make things more clear. (Essentially it could be said that all the UI instructions in your method are not performed immediately – they have to wait for the method to end, and then the main run loop picks them up and runs them.)

The code could better look like this:

- (void) startSomeLongOperation {
   [self createAndDisplayProgressSpinner];
   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_NORMAL, 0), ^{
       // …do something that takes long…
       dispatch_async(dispatch_get_main_queue(), ^{
            [self dismissProgressSpinner];
       });
   });
}

This moves the long operation into background, so that the main thread can continue executing immediately.

Upvotes: 10

Delete
Delete

Reputation: 922

I don't think you need to call

  [progressAlert addSubview:progressView];

Aside from that, it really should just appear...

Upvotes: 0

Related Questions