tags:

views:

42

answers:

3

The apple documentation made this seem like it was pretty easy but it's not working. My code is:

 UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

 [activity startAnimating];

 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://www.myserver.com"]]; 

[request setPostValue:name forKey:@"key"];


[request startSynchronous]; 
    NSLog(@"%@",[request responseString]);

 [activity stopAnimating];
 [activity release];

I didn't set anything up in UIBuilder because frankly, I don't understand exactly how it works with the UIActivityIndicator. Building and Running the app with the code I have above doesnt send off any warning indicators and runs just fine but I am not seeing the activity indicator.

+1  A: 

The UI won't update before finishing the method call (well or until the next iteration in the run loop to be more precisely), so this is effectively adding and removing it in the same step.

You should time your request asynchronously to see something happen, or at least schedule the different tasks (adding activity, the request itself and removing it) independently on the main thread. Blocking the main thread is a very bad idea though, and your app will get killed completely if your request takes too long to respond (for whatever reason - easy to think of with web services).

Eiko
I don't really understand. I am starting the animation before the request is sent - then sending the request (my actual request is much larger than the one i displayed but it's not necessary for fixing my problem - and THEN i stop animating. I tried to add [self.view addSubview:activity] right after startAnimating and that didnt fix it.
Rob
No. Any changes to UIKit won't take effect until the next iteration of the run loop. It won't even redraw a single pixel on screen between those calls.
Eiko
+2  A: 

@Eiko's right that your UI is going to block waiting on your synchronous web request to finish, but there's another problem. You're not even adding your activity view to your main view!

You need to give UIActivityIndicatorView *activity a .frame value, and then add it to the view heirarchy. Thusly:

activity.frame = CGRectMake(0,0,50,50); //that'll be top-left corner
[self.view addSubview:activity];
[activity startAnimating];

Now, it'll sit there NOT spinning while you do your web request, because you're doing that request on the main thread. I'm VERY glad you're using ASI, but you're going about it the ugly way.

Make your UIViewController conform to the ASIHttpRequestDelegate protocol.

Set request.delegate = self;.

Implement -requestFinished(ASIHttpRequest *)request to handle the response you get, and in THAT method, hide your activity view. You will PROBABLY wish at that point that you'd made it a named property, or at least an iVar, so you have a handle on it later.

Dan Ray
This is going to sound stupid but how do I get access to 'activity' from a different method?
Rob
You set it up as a synthesized property. And then when you instantiate it, you assign it to `self.activity` instead of `UIActivityIndicatorView *activity`.
Dan Ray
A: 

Try putting UIActivityIndicatorView creation in a different thread.

eviltrue