使用 Dropbox iOS API 和 NSDate 下载或上传文件不起作用

发布于 2024-12-26 18:55:38 字数 1497 浏览 3 评论 0原文

我正在使用 Dropbox iOS API 在不同设备之间同步。使用以下代码,我尝试比较文件修改的日期以下载/上传较新的文件。问题是,它只是下载,从不上传。有什么提示吗?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星軌x 2025-01-02 18:55:38

这是可疑的:

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

这总是评估为 true,这对我来说意味着 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");
}

另外,如果日期相等,我想你两者都不想做,对吧?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文