[英]A simple sync with the iPhone DropBox API
我對DropBox API感到有點沮喪。 它應該是簡單而直接的,但我還沒有簡單明了地解釋如何進行簡單的同步。
我按照DropBox API自述文件中的所有說明進行了操作。 為了測試整個事情,我創建了兩個按鈕來下載文件或從我的DropBox上傳文件。 這些文件位於我的應用文檔文件夾中。
這非常有效:
-(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];
}
但是,我現在想知道如何實現簡單的同步。 現在,我可以手動上傳和下載。 但我需要同步的是:
也許我只是太愚蠢了,但dropBox細節在某處如何實現這個相當簡單和直接的任務?
我知道有這個,但它並沒有給出任何代碼示例。
謝謝你的任何建議。
編輯
好的,所以我想我的第一步是找出在dropBox中找到的MyExample.txt的最后修改日期。
我寫了一個名為DBsync的奇妙方法,我只需輸入以下命令:
-(void) DBsync
{
[self.restClient loadMetadata:@"/myExamplePath"];
}
這將調用以下獲取元數據的方法。 這是對這篇文章的建議答案,我稍微評論了一下,以便明白發生的事情(如果有更多像我這樣愚蠢的人:
- (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*/
}
}
}
}
一旦我設法這樣做,我將發布下一步...
我認為你要找的是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*/
}
}
}
你不需要一個枚舉器,只需使用舊的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*/
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.