我已经为此苦苦挣扎了两天,但我无法让它发挥作用。我对此争论的部分原因是官方 Dropbox Sync API 教程在解释如何完成此操作方面做得非常差。
到目前为止,我可以使用 Dropbox API 编写任何内容,但获取任何内容本身就是一个完全不同的野兽。
我的应用程序委托的
- (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 中的文件,但下载它们比我预期的要复杂得多。
问题是我在初始同步未完成时尝试打开文件。你必须等到它完成。设置一个 NSThread 来不断检查你的文件系统的completeFirstSync是否完成。