Note that there are some explanatory texts on larger screens.

plurals
  1. POMethod gets called two times
    primarykey
    data
    text
    <p>I am doing a NSURLConnection that downloads a file only if there is a new one (checked with the last-modified date). But to accomplish this I am using two methods with two different NSURLRequests and NSURLConnection. Well, they do the same. </p> <pre><code> - (IBAction)uppdatera:(id)sender { checkHeaders = YES; NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.forellgatan.se/site/ftp_files/Kapareskolan.zip"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0]; NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if (theConnection) { self.receivedData = [[NSMutableData data] retain]; } else { UIAlertView *connectFailMessage = [[UIAlertView alloc] initWithTitle:@"Ingen internetanslutning! 1" message:@"Anslut dig till internet för att ladda ner!" delegate: self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [connectFailMessage show]; [connectFailMessage release]; } } - (void)downloadNewFile { NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.forellgatan.se/site/ftp_files/Kapareskolan.zip"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0]; NSURLConnection *theConnection2 = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if (theConnection2) { self.receivedData = [[NSMutableData data] retain]; } else { UIAlertView *connectFailMessage = [[UIAlertView alloc] initWithTitle:@"Ingen internetanslutning! 2" message:@"Anslut dig till internet för att ladda ner!" delegate: self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [connectFailMessage show]; [connectFailMessage release]; } checkHeaders = NO; self.progressView.hidden = NO; } </code></pre> <p>It goes through the didReceiveResponse method:</p> <pre><code>- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { if (checkHeaders == YES) { NSHTTPURLResponse *test = (NSHTTPURLResponse *)response; if ([test respondsToSelector:@selector(allHeaderFields)]) { NSDictionary *metaData = [test allHeaderFields]; NSString *lastModifiedString = [metaData objectForKey:@"Last-Modified"]; NSString *savedString = [[NSUserDefaults standardUserDefaults] stringForKey:@"LastModified"]; if (![lastModifiedString isEqualToString:savedString]) { [self downloadNewFile]; } else if ([lastModifiedString isEqualToString:savedString]) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Ingen uppdatering tillgänglig" message:@"Det finns ingen uppdatering att hämta just nu." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; [alert release]; } NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults]; [standardUserDefaults setObject:lastModifiedString forKey:@"LastModified"]; [standardUserDefaults synchronize]; } } [self.receivedData setLength:0]; self.fileSize = [[NSNumber numberWithLong: [response expectedContentLength]] retain]; } </code></pre> <p>Last the connectionDidFinishLaunching method:</p> <pre><code> - (void)connectionDidFinishLoading:(NSURLConnection *)connection { if (checkHeaders == NO) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *basePath = ([paths count] &gt; 0) ? [paths objectAtIndex:0] : nil; [self.receivedData writeToFile:[basePath stringByAppendingPathComponent:@"Kapareskolan.zip"] atomically:YES]; [self unzipDownloadedFile]; self.progressView.hidden = YES; NSLog(@"Finished..."); } [connection cancel]; } </code></pre> <p>I know the didFinishLaunching method gets called twice, but I want to know how I could get so the method doesn't get called twice if there is an update?</p> <p>I know it's a lot asked and much code here but just give me a hint and I'll be very thankful. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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