與服務器同步數據時,如何做到只更新被修改數據?如何節約流量,

RT,大致步驟如下:

  1. 1、Create a HTTP HEAD request.
  2. 2、Read the "Last-Modified" header and convert the string to a NSDate.
  3. 3、Read the last modification timestamp of the local file.
  4. 4、Compare the two timstamps.
  5. 5、Download the file from the server if it has been updated.
  6. 6、Save the downloaded file.
  7. 7、Set the last modification timestamp of the file to match the "Last-Modified" header on the server

  1. - (void)downloadFileIfUpdated {    
  2.     NSString *urlString = ... your URL ...    
  3.     DLog(@"Downloading HTTP header from: %@", urlString);    
  4.     NSURL *url = [NSURL URLWithString:urlString];    
  5.     
  6.     NSString *cachedPath = ... path to your cached file ...    
  7.     NSFileManager *fileManager = [NSFileManager defaultManager];    
  8.     
  9.     BOOL downloadFromServer = NO;    
  10.     NSString *lastModifiedString = nil;    
  11.     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];    
  12.     [request setHTTPMethod:@"HEAD"];    
  13.     NSHTTPURLResponse *response;    
  14.     [NSURLConnection sendSynchronousRequest:request returningResponse:&response error: nil];    
  15.     if ([response respondsToSelector:@selector(allHeaderFields)]) {    
  16.         lastModifiedString = [[response allHeaderFields] objectForKey:@"Last-Modified"];    
  17.     }    
  18.     
  19.     NSDate *lastModifiedServer = nil;    
  20.     @try {    
  21.         NSDateFormatter *df = [[NSDateFormatter alloc] init];    
  22.         df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";    
  23.         df.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];    
  24.         df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];    
  25.         lastModifiedServer = [df dateFromString:lastModifiedString];    
  26.     }    
  27.     @catch (NSException * e) {    
  28.         NSLog(@"Error parsing last modified date: %@ - %@", lastModifiedString, [e description]);    
  29.     }    
  30.     DLog(@"lastModifiedServer: %@", lastModifiedServer);    
  31.     
  32.     NSDate *lastModifiedLocal = nil;    
  33.     if ([fileManager fileExistsAtPath:cachedPath]) {    
  34.         NSError *error = nil;    
  35.         NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:cachedPath error:&error];    
  36.         if (error) {    
  37.             NSLog(@"Error reading file attributes for: %@ - %@", cachedPath, [error localizedDescription]);    
  38.         }    
  39.         lastModifiedLocal = [fileAttributes fileModificationDate];    
  40.         DLog(@"lastModifiedLocal : %@", lastModifiedLocal);    
  41.     }    
  42.     
  43.     // Download file from server if we don't have a local file    
  44.     if (!lastModifiedLocal) {    
  45.         downloadFromServer = YES;    
  46.     }    
  47.     // Download file from server if the server modified timestamp is later than the local modified timestamp    
  48.     if ([lastModifiedLocal laterDate:lastModifiedServer] == lastModifiedServer) {    
  49.         downloadFromServer = YES;    
  50.     }    
  51.     
  52.     if (downloadFromServer) {    
  53.         DLog(@"Downloading new file from server");    
  54.         NSData *data = [NSData dataWithContentsOfURL:url];    
  55.         if (data) {    
  56.             // Save the data    
  57.             if ([data writeToFile:cachedPath atomically:YES]) {    
  58.                 DLog(@"Downloaded file saved to: %@", cachedPath);    
  59.             }    
  60.     
  61.             // Set the file modification date to the timestamp from the server    
  62.             if (lastModifiedServer) {    
  63.                 NSDictionary *fileAttributes = [NSDictionary dictionaryWithObject:lastModifiedServer forKey:NSFileModificationDate];    
  64.                 NSError *error = nil;    
  65.                 if ([fileManager setAttributes:fileAttributes ofItemAtPath:cachedPath error:&error]) {    
  66.                     DLog(@"File modification date updated");    
  67.                 }    
  68.                 if (error) {    
  69.                     NSLog(@"Error setting file attributes for: %@ - %@", cachedPath, [error localizedDescription]);    
  70.                 }    
  71.             }    
  72.         }    
  73.     }    
  74. }    

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章