Question

I am using the Dropbox iOS API to sync between different devices. Using the following code, I am trying to compare the dates when the file was modified to either download/upload the newer file. The problem is, that it is just downloading and never uploading. Any hints?

- (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");
            }
        }
    }
}
Was it helpful?

Solution

This is suspect:

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

This is always evaluating to true, which means to me either that self.metaData.lastModifiedDate is equal to fileDate or that fileDate is always the earlier of the two dates.

Honestly, I'm having trouble even parsing these conditionals. What happens if you try evaluating it another way? like this, for instance:

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");
}

Also, if the dates are equal, I assume you want to do neither, right?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top