【问题标题】:A simple sync with the iPhone DropBox API与 iPhone DropBox API 的简单同步
【发布时间】:2011-05-10 12:41:36
【问题描述】:

我对 DropBox API 有点失望。它应该是简单而直接的,但我还没有遇到一个关于如何进行简单同步的简单明了的解释。

我按照 DropBox API 附带的自述文件中的所有说明进行操作。为了测试整个事情,我创建了两个按钮来从我的 DropBox 下载和上传文件。这些文件位于我的应用文档文件夹中。

这很好用:

    -(void) DBupload:(id)sender
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"MyExample.txt"];
    // NSError *error;
    [self.restClient uploadFile:@"MyExample.txt" toPath:@"/MyExamplePath" fromPath:filePath];
}

-(void) DBdownload:(id)sender
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"MyExample.txt"];
    NSError *error;
    [self.restClient loadFile:@"/myExamplePath/MyExample.txt" intoPath:filePath];
}

但是,我现在想知道如何实现简单的同步。现在,我可以手动上传和下载。但我需要同步的是:

  • 查看我的应用程序文件夹或 DropBox 文件夹中的 MyExample.txt 是否更新
  • 如果 App 文件夹中的 txt 是较新的:将其放入 Dropbox(覆盖旧的),即调用我的 DBupload 方法
  • 如果下拉框中的 txt 更新:将其下载到应用程序文件夹中,即调用我的 DBdownload 方法

也许我太笨了,但是 dropBox 是否在某处详细说明了如何完成这个相当简单直接的任务?

我知道有this,但它并没有真正提供任何代码示例。

感谢您的任何建议。


编辑

好的,所以我认为我的第一步是找出在 dropBox 中找到的 MyExample.txt 的最后修改日期。

我写了一个很棒的方法,叫做 DBsync,我在其中简单地输入了这个命令:

 -(void) DBsync
{
    [self.restClient loadMetadata:@"/myExamplePath"];

}

这会调用以下获取元数据的方法。这是对这篇文章的建议答案,我对它进行了一些评论,以便清楚地说明正在发生的事情(如果有更多像我这样的愚蠢的人:

 - (void)restClient:(DBRestClient*)client loadedMetadata:(DBMetadata*)metadata {

    NSLog(@"restClient:loadedMetadata function called");

    NSEnumerator *e= [metadata.contents objectEnumerator]; // indexes files in dropbox?
    DBMetadata *dbObject;   // loads metadate we need, e.g. lastModifiedDated
    int numberOfFiles = [metadata.contents count]; // counts files in DropBox - I guess we don't really need this
    NSLog(@"numberOfFiles %i", numberOfFiles); 

    while ((dbObject = [e nextObject])) { // this goes through every single file in the DropBox
        if (!dbObject.isDirectory) { // this determines whether we are talking about a file or a folder
            NSString *fileName = [dbObject.path lastPathComponent]; // this puts the name of the last component, e.g. in /MyExamplePath/MyExample.txt = MyExample.txt into fileName
            NSLog(@"File which is currently being checked: %@", fileName);

            if ([fileName isEqualToString:@"MyExample.txt"]) {  

                NSLog(@"Found it: %@", fileName);
                NSLog(@"Date last modified: %@", dbObject.lastModifiedDate); 

                /* to do: call dbupload if dbObject.lastModifiedDate > than your local file*/
            }
        }
    }
}

一旦我设法这样做,我将发布下一步......

【问题讨论】:

    标签: iphone objective-c cocoa-touch dropbox-api


    【解决方案1】:

    我认为您正在寻找的是 loadmetadata 方法。这是一个未经测试的示例:

    - (void)restClient:(DBRestClient*)client loadedMetadata:(DBMetadata*)metadata {
        NSEnumerator *e= [metadata.contents objectEnumerator];
        DBMetadata *dbObject;
        numberOfFiles = [metadata.contents count];
        while ((dbObject = [e nextObject])) {
        if (!dbObject.isDirectory) {
            NSString *fileName = [dbObject.path lastPathComponent];
            if (![fileName isEqualToString:@"MyExample.txt"]) {  
               /* call dbupload if dbObject.lastModifiedDate > than your local file*/
            }
        }
    }
    

    【讨论】:

    • 非常感谢。我相应地更新了我的帖子。在我接受您的回答之前,请先问一个问题:它必须是 if ([fileName... 而不是 if(![fileName... 对吗?我想这只是一个错字。另外,我如何获得我的日期本地文件?非常感谢您的帮助。我们距离如何进行简单同步的好教程只有几步之遥...
    • 不用担心日期比较,只需弄清楚如何进行。将相应地更新我的帖子。
    【解决方案2】:

    您不需要枚举器,只需使用旧的 for... 循环;)

    - (void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata {
        for (DBMetadata * child in metadata.contents) {
            if (!child.isDirectory) {
                NSString *fileName = [dbObject.path lastPathComponent];
                if (![fileName isEqualToString:@"MyExample.txt"]) {  
                    /* call dbupload if dbObject.lastModifiedDate > than your local file*/
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多