0

I had the UIActivityIndicatorView working fine in simulator and other 3.0 devices in my app. But I found out that it was not spinning (or showing) in the new iphone 4. Basically I need to show the activity indicator when a button is clicked and hide it when the button click event is complete. I was using the approach below.

[NSThread detachNewThreadSelector: @selector(spinBegin) toTarget:self withObject:nil];

from this link. As mentioned, it correctly spins the activity indicator on all except 4.*.. not sure why. To get around this, I also followed another approach something like (from developer.apple.com)

` (IBAction)syncOnThreadAction:(id)sender

{

[self willStartJob]; [self performSelectorInBackground: @selector(inThreadStartDoJob:) withObject:theJobToDo ]; 

}

(void)inThreadStartDoJob:(id)theJobToDo

{

NSAutoreleasePool * pool; NSString * status; pool = [[NSAutoreleasePool alloc] init]; assert(pool != nil); status = [... do long running job specified by theJobToDo ...] [self performSelectorOnMainThread: @selector(didStopJobWithStatus:) withObject:status waitUntilDone:NO ]; [pool drain]; 

}

`

The problem with this was that, it is showing the acitivityVIewIndicator spinning correctly (at least on the simulator) but after it stops, the built in activity indicator in the top bar (where it shows the battery% etc) is still spinning.

I'm new to objective C. I have finished my app completely but for this silly thing. I realize there is no way to display UIActivityView without starting another thread. and finally, just to rant, I don't understand why they have to make it so complicated. I mean they knew it was going to have this problem, why not provide a sample code everyone can use rather than deriving their own solutions.

Finally, can anyone please provide me with a direction or some sample code. I would really appreciate it. I have been searching for a few hours now and have not found anything really that works!

1 Answer 1

4

Why are you starting/stopping the indicator on a separate thread? Any methods you send to your UIActivityIndicatorView must be sent on the main (UI) thread.

Any events sent by a button pressed will automatically be run on the main thread. If you're using background threads to complete the process, you could do something like:

- (IBAction)buttonPressed:(id)sender { // This runs on the main thread [activityIndicator startAnimating]; [self performSelectorInBackground:@selector(inThreadStartDoJob:) withObject:theJobToDo]; } - (void)inThreadStartDoJob:(id)theJobToDo { // Set up autorelease pool ... // Run your long-running action ... // Stop the spinner. Since we're in a background thread, // we need to push this to the UI Thread [activityIndicator performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:YES]; } 

Edit: As for the activity indicator in the top bar (where the battery is), doesn't this automatically start/stop based on network activity?

Sign up to request clarification or add additional context in comments.

2 Comments

The last line of code that you have mentioned, [activityIndicator performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:YES]; did you mean [SELF performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:YES]; if so, this is exactly what i'm doing and it seems to work except for the activity indicator in the top bar.. I did not know that it was based on network activity, let me take a look.
i'll accept this answer. I ended up doing a [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; to not show the network indicator. I have network activity but thats all complete by the time I call the stop method. Not really sure what is going on there.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.