tags:

views:

224

answers:

2

what's wrong with my code?

i want spinner indicator start when view1.nib start to load. so i put [spinner startAnimating]; in - (void)viewDidLoad . but it will get that url then start spinner indicator...

- (void)viewDidLoad {








[spinner startAnimating];

NSURL *originalUrl=[NSURL URLWithString:@"http://example.com/"];
NSData *data=nil;  
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:originalUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
NSURLResponse *response;
NSError *error;
data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];


NSURL *LastURL=[response URL];


NSLog(@"%@",LastURL);



[backr,backrm,downloadr  setEnabled:FALSE];

backr.hidden=YES;
backrm.hidden=YES;
downloadr.hidden=YES;
[self nextr:0];
[super viewDidLoad];

}

+1  A: 

You're downloading the data synchronously on the main thread, so the UI will hang until the operation is complete. That is why your spinning only shows on completion.

Either run your download in a background thread, or use NSURLConnection's asynchonous methods.

Tom Irving
how can i run download in background ?
Naeim
Either [NSThread detachNewThreadSelector...] or take a look in the documentation for NSURLConnection. Personally I would choose the second option.
Tom Irving
A: 

Your problem is that block your main thread using the sendSynchronousRequest. While the data is downloading your thread is blocking and so your animation too, and after the request has been done the animation continue.

I should you to use the connectionWithRequest:delegate: or the initWithRequest:delegate: methods and set the delegate to self. You can find more information here: Using NSURLConnection


EDIT:

Example:

In your interface define it:

@interface YourInterface {
@private
   NSMutableData *receivedData;
}

then in your controller into the viewDidLoad:

// your previous definition of your NSMutableRequest

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if (theConnection) {
    // Create the NSMutableData that will hold
    // the received data
    // receivedData is declared as a method instance elsewhere
    receivedData=[[NSMutableData data] retain];
} else {
    // inform the user that the download could not be made
}

to finish, again in you controller, implement these methods:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    // append the new data to the receivedData
    // receivedData is declared as a method instance elsewhere
    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    // release the connection, and the data object
    [connection release];

    // receivedData is declared as a method instance elsewhere
    [receivedData release];

    // inform the user
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // do something with the data
    // receivedData is declared as a method instance elsewhere
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);

    // release the connection, and the data object
    [connection release];
    [receivedData release];
}
Yannick L.
how can i change this line for using connectionWithRequest:delegatedata = [NSURLConnection sendSynchronousRequest:request returningResponse:
Naeim
I have edit my post with an example.
Yannick L.