与iPhone DropBox API的简单同步

n.e*_*ind 5 iphone cocoa-touch objective-c dropbox-api

我对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];
}
Run Code Online (Sandbox Code Playgroud)

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

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

也许我只是太愚蠢了,但dropBox细节在某处如何实现这个相当简单和直接的任务?

我知道有这个,但它并没有给出任何代码示例.

谢谢你的任何建议.


编辑

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

我写了一个名为DBsync的奇妙方法,我只需输入以下命令:

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

}
Run Code Online (Sandbox Code Playgroud)

这将调用以下获取元数据的方法.这是对这篇文章的建议答案,我稍微评论了一下,以便明白发生的事情(如果有更多像我这样愚蠢的人:

 - (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*/
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

小智 2

我认为您正在寻找的是 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*/
        }
    }
}
Run Code Online (Sandbox Code Playgroud)