I have been struggling with this for two days now, but I cannot get it to work. Part of the reason I'm fighting with this is that the official Dropbox Sync API tutorial does a very poor job explaining how to get this done.
So far I can write anything using the Dropbox API, but getting anything is being a whole different beast on its own.
I have the following code (apologies if it is chaotic - I have been testing a lot and it's just natural it will clutter up sooner or later) in my app delegate's - (void)applicationDidBecomeActive:(UIApplication *)application
method:
if([[[DBAccountManager sharedManager] linkedAccount] isLinked])
{
DBError *erri = nil;
if(!self.collectionsFile.open)
{
DBPath *newPath = [[DBPath root] childPath:[NSString stringWithFormat:@"metadata/%@", @"user_collections.json"]];
//self.collectionsFile = [[DBFilesystem sharedFilesystem] createFile:newPath error:nil];
self.collectionsFile = [[DBFilesystem sharedFilesystem] openFile:newPath error:&erri];
}
DBFileStatus *newerStatus = self.collectionsFile.newerStatus;
DBFileStatus *status = self.collectionsFile.status;
NSLog(@"%@ erri", erri.localizedDescription);
__block NSString *contents = [self.collectionsFile readString:nil];
NSLog(@"Contents %@", contents);
[self.collectionsFile addObserver:self block:^(){
NSLog(@"Observer called");
if([[DBFilesystem sharedFilesystem] completedFirstSync])
{
NSLog(@"First sync done");
if(newerStatus != nil)
{
NSLog(@"File is downloading.");
}else
{
NSLog(@"Im here dude %@", contents);
NSString *metadata = [(FTIBAppDelegate *)[[UIApplication sharedApplication] delegate] getMetadataPath];
NSString *collectionsFile = [NSString stringWithFormat:@"%@/%@", metadata, @"user_collections.json", nil];
[contents writeToFile:collectionsFile atomically:NO encoding:NSUTF8StringEncoding error:nil];
}
if(status.cached)
{
NSString *metadata = [(FTIBAppDelegate *)[[UIApplication sharedApplication] delegate] getMetadataPath];
NSString *collectionsFile = [NSString stringWithFormat:@"%@/%@", metadata, @"user_collections.json", nil];
[contents writeToFile:collectionsFile atomically:NO encoding:NSUTF8StringEncoding error:nil];
}
}
}];
}
Whenever I reinstall my app and link Dropbox again, I get this beautiful output:
2014-01-21 15:58:24.171 Mignori[913:60b] App linked successfully!
2014-01-21 15:58:24.301 Mignori[913:60b] [WARNING] ERR:
DROPBOX_ERROR_USAGE: sync.cpp:210: Checking file path before file
types info has been fetched. Wait for first sync to avoid creating a
file which may fail to upload later. 2014-01-21 15:58:24.473
Mignori[913:60b] [ERROR] ERR: DROPBOX_ERROR_ALREADYOPEN: file.cpp:188:
p(/v1/t5.json) already open (1) 2014-01-21 15:58:24.528
Mignori[913:60b] DropboxSync error - Error Domain=dropbox.com
Code=2004 "The operation couldn’t be completed. (dropbox.com error
2004.)" UserInfo=0x17028ba0 {desc=file.cpp:188: p(/v1/t5.json) already open (1)} 2014-01-21 15:58:24.531 Mignori[913:60b] The operation
couldn’t be completed. (dropbox.com error 2004.) erri 2014-01-21
15:58:24.532 Mignori[913:60b] Contents (null)
A couple of things to keep in mind:
I will appreciate any help to get this to work. Even a (better) sample code on how to get this done. I can update files in Dropbox with no problem at all, but downloading them is being much more complicated than I expected.
The problem was that I was trying to open the file when the initial sync wasn't completed. You have to wait until it's done. Set an NSThread to constantly check if your filesystem's completeFirstSync is completed.