user3290180
user3290180

Reputation: 4410

iOS: how to properly stop an Activity Indicator?

I'd like to stop the animation of the indicator within a method called by default NSNotificationCenter with a postNotificationName. So I'm doing this on Main Thread

-(void)method
{
    ...
    [ind performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:NO];
}

It does'n work. Method is called correctly, any other called selectors do their job but not stopAnimating. I put [ind stopAnimating] in another function and then called it via performSelectorOnMainThread but it still didn't worked.

Upvotes: 3

Views: 1578

Answers (3)

Sanjay Mohnani
Sanjay Mohnani

Reputation: 5967

You can also use the below method which starts and stops the activity indicator on main thread in a single method, also provides you to execute your code asynchronously as well-

- (void)showIndicatorAndStartWork
{
    // start the activity indicator (you are now on the main queue)
    [activityIndicator startAnimating];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // do your background code here

        dispatch_sync(dispatch_get_main_queue(), ^{
            // stop the activity indicator (you are now on the main queue again)  
        [activityIndicator stopAnimating];
        });
    });
}

Upvotes: 3

Mayur Deshmukh
Mayur Deshmukh

Reputation: 1169

Try this...

Create a method that stops your animation

-(void)stopAnimationForActivityIndicator
{
    [ind stopAnimating];
}

Replace your method like this -

-(void)method
{
    ...
    [self performSelectorOnMainThread:@selector(stopAnimationForActivityIndicator) withObject:nil waitUntilDone:NO];
}

Should do the magic...

Upvotes: 5

itsji10dra
itsji10dra

Reputation: 4675

Try :

-(void)method
{
    dispatch_async(dispatch_get_main_queue(), ^{ 
       [ind stopAnimating];   
    });
}

Upvotes: 2

Related Questions