Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use <code>NSURLRequest</code> object to specify a timeout for evrey request you did for download by using this <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/Reference/Reference.html#//apple_ref/occ/clm/NSURLRequest/requestWithURL%3acachePolicy%3atimeoutInterval%3a" rel="nofollow"><code>requestWithURL:cachePolicy:timeoutInterval:</code></a> method.</p> <p>Please check whether your <code>NSURLConnection</code>'s delegate is set and responds to the <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/connection%3adidFailWithError%3a" rel="nofollow"><code>connection:didFailWithError:</code></a> method. A <code>NSURLConnection</code> calls either this method or <code>connectionDidFinishLoading:</code> upon connection completion.</p> <p>Handle 'didFailWithError' method and check the reason for failer by using <code>NSError</code> object.</p> <p>But if you get response from server and response time is slow, then used <code>NSTimer</code>. Create Helper class for downloading data so you can reuse the class for multiple downloads by creating several instances and set NSTimer in it, if download finish within 30 seconds invalidate timer else cancel downloading <code>[self.connection cancel]</code>.</p> <p>Please check following code:</p> <pre><code>- (void)_startReceive // Starts a connection to download the current URL. { BOOL success; NSURL * url; NSURLRequest * request; // Open a connection for the URL. request = [NSURLRequest requestWithURL:url]; assert(request != nil); self.connection = [NSURLConnection connectionWithRequest:request delegate:self]; assert(self.connection != nil); // If we've been told to use an early timeout for get complete response within 30 sec, // set that up now. self.earlyTimeoutTimer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(_earlyTimeout:) userInfo:nil repeats:NO]; } } - (void)_stopReceiveWithStatus:(NSString *)statusString // Shuts down the connection and displays the result (statusString == nil) // or the error status (otherwise). { if (self.earlyTimeoutTimer != nil) { [self.earlyTimeoutTimer invalidate]; self.earlyTimeoutTimer = nil; } if (self.connection != nil) { [self.connection cancel]; self.connection = nil; } } - (void)_earlyTimeout:(NSTimer *)timer // Called by a timer (if the download is not finish) { [self _stopReceiveWithStatus:@"Early Timeout"]; } - (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)response // A delegate method called by the NSURLConnection when the request/response // exchange is complete. { } - (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data // A delegate method called by the NSURLConnection as data arrives. We just // write the data to the file. { } - (void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error // A delegate method called by the NSURLConnection if the connection fails. { NSLog(@"didFailWithError %@", error); // stop Receive With Status Connection failed } - (void)connectionDidFinishLoading:(NSURLConnection *)conn // A delegate method called by the NSURLConnection when the connection has been // done successfully. We shut down the connection with a nil status. { NSLog(@"connectionDidFinishLoading"); // If control reach here before timer invalidate then save the data and invalidate the timer if (self.earlyTimeoutTimer != nil) { [self.earlyTimeoutTimer invalidate]; self.earlyTimeoutTimer = nil; } }</code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload