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.
我对DropBox API感到有点沮丧。它应该是简单而直接的,但我还没有简单而简单地解释如何进行简单的同步。
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.
我按照DropBox API自述文件中的所有说明进行了操作。为了测试整个事情,我创建了两个按钮来下载文件或从我的DropBox上传文件。这些文件位于我的应用文档文件夹中。
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
- 找出我的应用程序文件夹或我的DropBox文件夹中的MyExample.txt是否更新
- if the txt in the App's folder is more recent: drop it into dropbox (overriding the old), i.e. call my DBupload method
- 如果应用程序文件夹中的txt更新:将其放入dropbox(覆盖旧文件夹),即调用我的DBupload方法
- if the txt in the drop box is more recent: download it into the apps folder, i.e. call my DBdownload method
- 如果下拉框中的txt更新:将其下载到apps文件夹中,即调用我的DBdownload方法
Perhaps I am just too dumb, but does dropBox detail somewhere how to achieve this rather simple and straight forward task?
也许我只是太愚蠢,但在某处如何实现这个相当简单和直接的任务dropBox细节?
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.
好的,所以我想我的第一步是找出在dropBox中找到的MyExample.txt的最后修改日期。
I wrote a wonderful method called DBsync in which I simply put this command:
我写了一个名为DBsync的奇妙方法,我只需输入以下命令:
-(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...
一旦我设法这样做,我将发布下一步...
2 个解决方案
#1
2
I think what you are looking for is the loadmetadata method. Here is an untested example:
我认为你要找的是loadmetadata方法。这是一个未经测试的例子:
- (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
0
You don't need an enumerator, just use the good old for... loop ;)
你不需要一个枚举器,只需使用旧的for ...循环;)
- (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*/
}
}
}
}
#1
2
I think what you are looking for is the loadmetadata method. Here is an untested example:
我认为你要找的是loadmetadata方法。这是一个未经测试的例子:
- (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
0
You don't need an enumerator, just use the good old for... loop ;)
你不需要一个枚举器,只需使用旧的for ...循环;)
- (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*/
}
}
}
}