user1331999
user1331999

Reputation:

Wait until UI has updated before removing UIActivityIndicator

I am having an issue with an iPad app I am developing. I have the following code:

CFTimeInterval startTime = CFAbsoluteTimeGetCurrent();

[self showSpinnerWithMessage:@"Loading settings..."]; // Shows a UIActivityIndicator on screen

dispatch_async(dispatch_get_global_queue(0, 0), ^
{
    //Updating items on screen here
    [label1 setText:@"Test1"];
    [label2 setText:@"Test3"];
    //...

    CFTimeInterval difference = CFAbsoluteTimeGetCurrent() - startTime;

    if (difference < MINIMUM_INDICATOR_SECONDS)
    {
        [NSThread sleepForTimeInterval:MINIMUM_INDICATOR_SECONDS - difference];
    }

    dispatch_async(dispatch_get_main_queue(), ^(void)
    {
        [self removeSpinner]; // Removes UIActivityIndicator from screen
    });
};

The problem is that sometimes the UI takes a while to update, and in these particular instances the spinner (UIActivityIndicator) goes away before the UI has actually updated. I realize this is because the items on screen do not actually update until the next run loop so my question is, how can make my [self removeSpinner] call wait until the UI has updated?

Upvotes: 0

Views: 415

Answers (2)

Husker Jeff
Husker Jeff

Reputation: 857

Try putting the UI-updating code in the block with removeSpinner.

dispatch_async(dispatch_get_main_queue(), ^(void)
{
    [label1 setText:@"Test1"];
    [label2 setText:@"Test3"];
    [self removeSpinner]; // Removes UIActivityIndicator from screen
});

Upvotes: 2

dasdom
dasdom

Reputation: 14063

Put a [self.view setNeedsDisplay]; just before the [self removeSpinner].

Upvotes: 0

Related Questions