与服务器同步数据时如何节约流量

 

High-level steps:

  1. Create a HTTP HEAD request.
  2. Read the "Last-Modified" header and convert the string to a NSDate.
  3. Read the last modification timestamp of the local file.
  4. Compare the two timstamps.
  5. Download the file from the server if it has been updated.
  6. Save the downloaded file.
  7. Set the last modification timestamp of the file to match the "Last-Modified" header on the server- (void)downloadFileIfUpdated { NSString *urlString = ... your URL ... DLog(@"Downloading HTTP header from: %@", urlString); NSURL *url = [NSURL URLWithString:urlString]; NSString *cachedPath = ... path to your cached file ... NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL downloadFromServer = NO; NSString *lastModifiedString = nil; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"HEAD"]; NSHTTPURLResponse *response; [NSURLConnection sendSynchronousRequest:request returningResponse:&response error: nil]; if ([response respondsToSelector:@selector(allHeaderFields)]) { lastModifiedString = [[response allHeaderFields] objectForKey:@"Last-Modified"]; } NSDate *lastModifiedServer = nil; @try { NSDateFormatter *df = [[NSDateFormatter alloc] init]; df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'"; df.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]; df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; lastModifiedServer = [df dateFromString:lastModifiedString]; } @catch (NSException * e) { NSLog(@"Error parsing last modified date: %@ - %@", lastModifiedString, [e description]); } DLog(@"lastModifiedServer: %@", lastModifiedServer); NSDate *lastModifiedLocal = nil; if ([fileManager fileExistsAtPath:cachedPath]) { NSError *error = nil; NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:cachedPath error:&error]; if (error) { NSLog(@"Error reading file attributes for: %@ - %@", cachedPath, [error localizedDescription]); } lastModifiedLocal = [fileAttributes fileModificationDate]; DLog(@"lastModifiedLocal : %@", lastModifiedLocal); } // Download file from server if we don't have a local file if (!lastModifiedLocal) { downloadFromServer = YES; } // Download file from server if the server modified timestamp is later than the local modified timestamp if ([lastModifiedLocal laterDate:lastModifiedServer] == lastModifiedServer) { downloadFromServer = YES; } if (downloadFromServer) { DLog(@"Downloading new file from server"); NSData *data = [NSData dataWithContentsOfURL:url]; if (data) { // Save the data if ([data writeToFile:cachedPath atomically:YES]) { DLog(@"Downloaded file saved to: %@", cachedPath); } // Set the file modification date to the timestamp from the server if (lastModifiedServer) { NSDictionary *fileAttributes = [NSDictionary dictionaryWithObject:lastModifiedServer forKey:NSFileModificationDate]; NSError *error = nil; if ([fileManager setAttributes:fileAttributes ofItemAtPath:cachedPath error:&error]) { DLog(@"File modification date updated"); } if (error) { NSLog(@"Error setting file attributes for: %@ - %@", cachedPath, [error localizedDescription]); } } } } }  

 

你可能感兴趣的:(与服务器同步数据时如何节约流量)