2013-05-10 60 views
1

我試圖將我的應用程序中創建的文件同步到Dropbox,但似乎只有在應用程序退出後纔會發生同步,而不是實時創建文件並在位置間移動在應用中的不同文件夾中或創建/刪除。例如,我是否需要撥打某個電話?感謝你的幫助!iOS應用程序退出前的Dropbox同步

下面是我使用的同步代碼:

-(void)createFilePathinFolder:(NSString *)folderName FileName:(NSString *)fileName { 

NSFileManager *fileManager = [NSFileManager defaultManager]; 

NSString *folder = [self localDocumentsRootPath]; 

if (![folderName isEqualToString:@"root"]) { 
    folder = [folder stringByAppendingPathComponent:folderName]; 
} 

NSString *file = [folder stringByAppendingPathComponent:fileName]; 

if (![fileManager fileExistsAtPath:file]) { 
    [fileManager createFileAtPath:file contents:[@"0" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil]; 
} 

//Insert to FileTable 
[[DBHelper shared]insertToFileTableWithFolder:folderName FileName:fileName MetaFileName:nil Tag:nil Title:nil]; 

if ([NetworkHelper shared].canSyncWithCloud) { 
    NSString *filePathStr = [folderName stringByAppendingPathComponent:fileName];; 

    if ([folderName isEqualToString:@"root"]) { 
     filePathStr = fileName; 
    } 

    DBPath *filePath = [[DBPath root] childPath:filePathStr]; 
    DBError *error; 
    DBFile *destFile =[[DBFilesystem sharedFilesystem] createFile:filePath error:&error]; 

    NSData *fileData = [NSData dataWithContentsOfFile:file]; 

    [destFile writeData:fileData error:&error]; 

    //[destFile writeContentsOfFile:file shouldSteal:NO error:&error]; 

    [destFile close]; 

    if (error) { 
     NSLog(@"Error when creating file %@ in Dropbox, error description:%@", fileName, error.description); 
    } 
    } 
} 

回答

0

你的錯誤檢查是完全錯誤的。您的代碼應該更像這樣:

DBPath *filePath = [[DBPath root] childPath:filePathStr]; 
DBError *error = nil; 
DBFile *destFile =[[DBFilesystem sharedFilesystem] createFile:filePath error:&error]; 
if (destFile) { 
    NSData *fileData = [NSData dataWithContentsOfFile:file]; 
    if (![destFile writeData:fileData error:&error]) { 
     NSLog(@"Error when writing file %@ in Dropbox, error description: %@", fileName, error); 
    } 
    [destFile close]; 
} else { 
    NSLog(@"Error when creating file %@ in Dropbox, error description: %@", fileName, error); 
} 

該文件應該立即與您擁有的代碼同步。這假定您已將您的應用正確鏈接到一個帳戶和所有。

您使用的是什麼版本的Dropbox Sync API? 1.0.7有一些潛在的網絡問題。我有一個1.0.8的beta版,似乎解決了這些問題。你可能需要等到1.0.8出來。

您可以驗證Dropbox是否掛起。在調試器中運行應用程序時,在創建文件後等待一會兒。如果文件沒有出現,請在調試器中暫停您的應用程序並查看所有線程。您應該看到一個或多個與Dropbox相關的線程。如果看起來被dbx_cfhttp_request引用阻塞,那麼您在Dropbox框架中遇到了一個錯誤。將您的設備置於飛行模式10-15秒,然後再次關閉飛行模式應將其重新啓動。