A simple sync with the iPhone DropBox API

北城以北 提交于 2019-12-18 12:38:18

问题


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.

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.

This works splendidly:

    -(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:

  • 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

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.

Thanks for any suggestions.


EDIT

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.

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...


回答1:


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



回答2:


You don't need an enumerator, just use the good old for... loop ;)

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


来源:https://stackoverflow.com/questions/5950168/a-simple-sync-with-the-iphone-dropbox-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!