coder
coder

Reputation: 10520

UIActivityIndicator not animating

I've been beating my head against a wall with this problem, and need some help. I am trying to display a UIActivityIndicator while I load data in the background. I'm not sure if this is relevant or not, but I am loading a tableview. The indicator appears, but doesn't spin...unless I touch the screen, or something else happens while loading-like if I receive a text message. Here is the code:

UIActivityIndicatorView  *av = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
av.frame=CGRectMake(145, 160, 25, 25);
av.tag  = 1;
[self.mTableView addSubview:av];
[av startAnimating];
[self performSelectorInBackground:@selector(load) withObject:nil];

I've also tried this:

UIActivityIndicatorView  *av = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
av.frame=CGRectMake(145, 160, 25, 25);
av.tag  = 1;
[self.view addSubview:av];
[av startAnimating];
[self performSelectorInBackground:@selector(load) withObject:nil];

and have tried it with commenting out the last line-So not having the background thread run. I have tried both versions of the code in my viewDidLoad and viewDidAppear methods.

Any ideas?

Edit Here is my load method

- (void)load {

NSString *post = [NSString stringWithFormat:@"id[]=%@", [ids objectAtIndex:0]];

for(int i = 1; i < ids.count; i++){
    post = [NSString stringWithFormat:@"%@&id[]=%@", post, [ids objectAtIndex:i]];
}


NSURL *url=[NSURL URLWithString:@"http://myurl/instructions"];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

/* when we user https, we need to allow any HTTPS cerificates, so add the one line code,to tell teh NSURLRequest to accept any https certificate, i'm not sure about the security aspects
 */

//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

[self parseData:data];

[spinner stopAnimating];

}

Upvotes: 2

Views: 3318

Answers (4)

coder
coder

Reputation: 10520

The UI needs time to load, and can't start the animation immediately. By starting it with an NSTimer in the viewDidLoad method, the problem was solved.

timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(spin) userInfo:nil repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:UITrackingRunLoopMode];

and the scan method:

spinner = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
spinner.center = self.view.center;
spinner.hidesWhenStopped = YES;
[spinner startAnimating];
[self.view addSubview:spinner];
if ([spinner isAnimating]) NSLog(@"animating");
else NSLog(@"not animating");

[self performSelectorInBackground:@selector(load) withObject:nil];

Hopefully this helps.

Upvotes: 1

danh
danh

Reputation: 62686

Not related to the activity indicator, but your load method rewrites the post variable in a loop. I think you intend to concatenate:

post = [post stringByAppendingFormat: ...]

Otherwise your web service will see only the last param.

Also, once you get your spinner animating, the next problem you'll have is that it won't stop because stopAnimating is being called off the main thread.

Upvotes: 0

Olie
Olie

Reputation: 24675

It's not clear where you're adding the activity indicator but, if it's not on the main thread, then UI calls to it may not work. I typically set up a separate routine to start & stop my activity monitors, so I can performSelectorInMainThread: them.

Upvotes: 0

strings42
strings42

Reputation: 543

You didn't include enough code to see where you're stopping the UIActivityIndicator animation and what's going on while you're displaying it, but the problem almost certainly is one of the following:

1) You're expecting your code to wait for some asynchronous method, which is causing your activity indicator to get shut off prematurely,

or

2) Both the "background" task and the UIActivityIndicator are running on the same thread, and your "background" task is monopolizing the thread to the point where the activity indicator doesn't get enough time to animate.

This post provides an example of how to push the UIActivityIndicator into its own thread:

Activity Indicator doesn't spin

Upvotes: 2

Related Questions