Pregunta

I have a problem about order of executing methods.

if (indexPath.row == 2) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                    message:@"Data will be downloaded"
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];

    if([app getData]){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                        message:@"Data is downloaded."
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }   
}

When I run this code snippet, I want to first show an alert view. However, it calls the getData method before showing alert view. Once the getData method is completed, alertView comes to the window.

How can I correct this?

¿Fue útil?

Solución 2

I don't have Xcode with me, but I'll take a stab at it anyway. Your problem is because the alert won't show until the main loop iterates. And it won't iterate until your getData method is executed since you're doing this on the main thread. So you need to fork.

Try wrapping your if() in something like this:

dispatch_async(dispatch_get_main_queue(),
^ {
    if([app getData]){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                    message:@"Data is downloaded."
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
        [alert show];
    }   
});

Otros consejos

The function is called asynchronously therefore appData gets called before the first alert view is visible. Change your code to this:

if (indexPath.row == 2) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                message:@"Data will be downloaded"
                                               delegate:self
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
[alert show];

} The below method will be called when the user presses OK on your first alert, but it means that while the first alert is visible, your code for appData would not start.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if([app getData]){
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                    message:@"Data is downloaded."
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}   

}

Note: Remember to add UIAlertViewDelegate in your view controller

It's because the run loop must continue running so [UIAlertView show] is asynchronous and will not block the current thread. If the main thread was blocked then no user interface events could be delivered.

If you want something to occur after the first alert view is dismissed then do it within the alertView:clickedButtonAtIndex: delegate method.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top