CoreCode
CoreCode

Reputation: 2227

UIActivityView not animating

I have a button on the iPhone interface that performs a function. The function is this:

-(IBAction)courses:(id)sender {
[loading startAnimating];
[connection getCourses];
[outlet setText:connection.lastResponseData];
}

My problem is that the animation only starts after the [outlet setText:connection.lastResponseData. How can I get the loading to start animating before that?

Thanks.

Upvotes: 1

Views: 78

Answers (2)

Ash Furrow
Ash Furrow

Reputation: 12421

It looks like getCourses is blocking the main thread; any animations you start will be deleted until after courses: returns. Modify your code to use a background thread:

-(IBAction)courses:(id)sender {
[loading startAnimating];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [connection getCourses];
        dispatch_async(dispatch_get_main_queue(), ^{
            [outlet setText:connection.lastResponseData];       
            [loading stopAnimating];
        });
    });
}

This will put your blocking network access in a system-provided background GCD queue, which wraps a background thread.

Upvotes: 2

A-Live
A-Live

Reputation: 8944

Seems that your connection blocks the main thread, which is also responsible for the UI. Try to interact with UI while the connection is started but not finished, if it doesn't respond - make the connection running in another thread.


For example, asynchronous NSURLConnection will not block your UI.

Upvotes: 0

Related Questions