Easy sync with iPhone DropBox API

I'm a little upset with the DropBox API. Everything is supposed to be simple and straightforward, but I still have to go through a simple and simple explanation of how to make simple synchronization.

I followed all the instructions that you can find in the readme that comes with the DropBox API. To test all this, I created two buttons to upload and download a file from or to DropBox. Files are located in the application's document folder.

This works great:

-(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, now I am wondering how to achieve simple synchronization. Right now, I can manually download and upload. But I need to sync:

  • find out if the MyExample.txt file in my application folder or in my DropBox folder is later
  • if txt in the App folder is later: drop it in the dropbox (overriding the old one), i.e. call my DBupload method
  • if txt in the drop-down list is later: upload it to the application folder, i.e. call my DBdownload method

Maybe I'm just too dumb, but somewhere DropBox details, how to achieve this rather simple and direct task?

I know there is this , but actually it does not give any code samples.

Thanks for any suggestions.


EDIT

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

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

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

This calls the following method, which receives metadata. This was the suggested answer to this post, and I commented a bit on this so that it is clear what is happening (if such stupid people like me:

  - (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, eg 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, eg 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 as soon as I succeed ...

+5
source share
2 answers

I think you are looking for the loadmetadata method. The following is an unverified 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
source

You do not need an enumerator, just use the good old one 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*/ } } } } 
0
source

All Articles