与iPhone DropBox API的简单同步 [英] A simple sync with the iPhone DropBox API

查看:112
本文介绍了与iPhone DropBox API的简单同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对DropBox API有点沮丧。它应该是所有简单和直接的,但我还没有来一个简单和平淡的解释如何做一个简单的同步。

I am getting slightly frustrated with the DropBox API. It is supposed to be all simple and straight forward, but I have yet to come a across a simple and plain explanation of how to do a simple sync.

我遵循了DropBox API中的readme中的所有指令。为了测试整个事情,我创建了两个按钮来下载和上传文件从或到我的DropBox。

I followed all the instruction you can find in the readme which comes withe DropBox API. To test the whole thing, I have created two buttons to download and upload a file from or to my DropBox. The files are found in my app documents folder.

这项工作非常精彩:

    -(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];
}

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

However, I am now wondering how to achieve a simply sync. Right now, I can manually upload and download. But what I need in order to sync is to:


  • 查看我的应用程序文件夹或DropBox文件夹中的MyExample.txt是否

  • 如果应用程序文件夹中的txt更新,请将其放入收存箱(覆盖旧邮件),即调用我的DBupload方法
  • 如果下拉框中的txt更新:将其下载到apps文件夹,即调用我的DBdownload方法
  • find out if the MyExample.txt in my App's folder or in my DropBox folder is more recent
  • if the txt in the App's folder is more recent: drop it into dropbox (overriding the old), i.e. call my DBupload method
  • if the txt in the drop box is more recent: download it into the apps folder, i.e. call my DBdownload method

太笨了,但是dropBox的细节在哪里如何实现这个相当简单和直接的任务?

Perhaps I am just too dumb, but does dropBox detail somewhere how to achieve this rather simple and straight forward task?

我知道有,但它并不真正给出任何代码示例。

I know that there is this but it doesn't really give any code samples.

感谢您的任何建议。

确定,我想我的第一步是找出在dropBox中找到的MyExample.txt的最后修改日期。

OK, so I figured that my first step is to find out the last modified date of MyExample.txt which is found in the dropBox.

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

I wrote a wonderful method called DBsync in which I simply put this command:

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

}

这会调用以下方法获取元数据。这是一个建议的答案这篇文章,我评论了一点,以便使它清楚发生了什么(如果有更多的蠢的人像我自己:

This calls the following method which gets the metadata. This was a suggested answer to this post, and I commented it a bit out so as to make it plain what is happening (if there are more dumb people like myself:

 - (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*/
            }
        }
    }
}

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

I will post the next step once I managed to do so...

推荐答案

寻找是loadmetadata方法。这是一个未经测试的例子:

I think what you are looking for is the loadmetadata method. Here is an untested example:

- (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*/
        }
    }
}

这篇关于与iPhone DropBox API的简单同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆