我已经在这两天苦苦挣扎了,但是我无法让它发挥作用。我正在与之抗争的部分原因是正式的Dropbox Sync API教程在解释如何完成这项工作方面做得很差。
到目前为止,我可以使用Dropbox API编写任何内容,但获取任何内容本身就是一个完全不同的野兽。
我有以下代码(道歉,如果它是混乱的 - 我已经测试了很多,它很自然会迟早会混乱)在我的app委托的- (void)applicationDidBecomeActive:(UIApplication *)application
方法中:
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]; } } }]; }
每当我重新安装我的应用并再次链接Dropbox时,我都会得到这个漂亮的输出:
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)
要记住以下几点:
我将非常感谢帮助您实现这一目标。甚至是关于如何完成这项工作的(更好的)示例代码。我可以毫无问题地更新Dropbox中的文件,但是下载它们比我预期的要复杂得多。
答案 0 :(得分:2)
问题是我在初始同步未完成时尝试打开文件。你必须等到它完成。设置NSThread以不断检查文件系统的completeFirstSync是否已完成。