2
votes

When a UIButton is triggered, a UIActivityIndicator is started, and then stopped when implementLogin finishes:

-(IBAction)loginButton {
    NSLog(@"loginButton triggered");

    // Checks the Fields are not empty 
    if ([sessionIdField.text length] != 0 && [usernameField.text length] != 0 ) {
        NSLog(@"Beginning Spinner");
        // Displays spinner
        [activitySpinner startAnimating];

        [self implementLogin]; 

        // Displays spinner
        [activitySpinner stopAnimating];
    }
}

However at runtime, the spinner doesn't appear! I have set the spinner to 'hide when stopped'.

When I set the activity indicator to animate before the view loads, it appears as it should, so I'm guessing it has a problem with the UIButton... (Also, I'm using 'Touch Up Inside' for the button.)

It's a simple problem... Can anyone help?

Thanks

2
Is implementLogin synchronous or not? - Costique
Yes implementLogin is synchronous. - Tim Windsor Brown

2 Answers

2
votes

Whatever implementLogin is doing (making a network request, perhaps?), it's doing it on the main thread, which is likely blocking UI updates like spinner animation.

You could recode something like this:

[activitySpinner startAnimating];
dispatch_async(dispatch_get_global_queue(DISPATCH_PRIORITY_DEFAULT, 0), ^{
    [self implementLogin]; 
    dispatch_async(dispatch_get_main_queue(), ^{
        // Stops spinner
        [activitySpinner stopAnimating];
    }
}

[Code is untested, but you get the idea.]

What is happening here is that you are dispatching the login task to the background, and the last thing that block will do is stop the spinner on the main thread (as a separate task).

0
votes

Without more code, one can only guess the reason why it doesn't work.

I assume 'Beginning Spinner' output correctly(?)

If so, you probably didn't properly initialize the UIActivityIndicatorView. Does it look like this?

activitySpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activitySpinner.hidesWhenStopped = YES;
[view addSubview:activitySpinner];