2012-01-15 38 views
1

我正在使用Dropbox iOS API在不同設備之間進行同步。使用下面的代碼,我試圖比較文件被修改爲下載/上傳新文件的日期。問題是,它只是下載並從不上傳。任何提示?使用Dropbox iOS API和NSDate下載或上傳文件不起作用

- (void)dropboxAuth { 
    if (![[DBSession sharedSession] isLinked]) { 
     [[DBSession sharedSession] link]; 
    } 
    else { 
     NSString *filename = @"NotesList.plist"; 
     NSString *destDir = @"/"; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); 
     NSString *documentsDir = [paths objectAtIndex:0]; 
     NSString *address = [documentsDir stringByAppendingPathComponent:@"NotesList.plist"]; 

     [[self restClient] loadMetadata:@"/"]; 

     if([[NSFileManager defaultManager] fileExistsAtPath:address]) { 
      NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:address error:&error]; 
      NSDate *fileDate =[dictionary objectForKey:NSFileModificationDate]; 

      if ([[fileDate earlierDate:self.metaData.lastModifiedDate]isEqualToDate:fileDate]) { 
       [self.restClient loadFile:[NSString stringWithFormat: @"%@/%@", destDir, filename] 
           intoPath:address]; 
       NSLog(@"Downloading"); 
      } 
      else if ([[self.metaData.lastModifiedDate earlierDate:fileDate] isEqualToDate:self.metaData.lastModifiedDate]) { 
       [[self restClient] uploadFile:filename toPath:destDir fromPath:address]; 
       NSLog(@"Uploading"); 
      } 
     } 
    } 
} 
+0

找到一個有用的帖子:http://stackoverflow.com/questions/5950168/a-simple-sync-with-the-iphone-dropbox-api – mstottrop 2012-04-22 14:05:01

回答

2

這是犯罪嫌疑人:

if ([[fileDate earlierDate:self.metaData.lastModifiedDate]isEqualToDate:fileDate]) { 

這是總爲真,這對我意味着要麼self.metaData.lastModifiedDate等於fileDatefileDate是兩個日期總是較早。老實說,即使解析這些條件,我也遇到了麻煩。如果你嘗試以另一種方式評估它會發生什麼?像這樣,例如:

if (nil == fileDate || fileDate.timeIntervalSinceReferenceDate < self.metaData.lastModifiedDate.timeIntervalSinceReferenceDate) 
{ 
    [self.restClient loadFile:[NSString stringWithFormat: @"%@/%@", destDir, filename] 
         intoPath:address]; 
    NSLog(@"Downloading"); 
} 
else if (nil != fileDate && fileDate.timeIntervalSinceReferenceDate > self.metaData.lastModifiedDate.timeIntervalSinceReferenceDate) { 
    [[self restClient] uploadFile:filename toPath:destDir fromPath:address]; 
    NSLog(@"Uploading"); 
} 

另外,如果日期相等,我假設你都不想這樣做,對嗎?