Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A simple sync with the iPhone 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.

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

like image 244
n.evermind Avatar asked Nov 05 '22 22:11

n.evermind


1 Answers

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*/
        }
    }
}
like image 158
jmoffett Avatar answered Nov 11 '22 08:11

jmoffett