2012-02-16 67 views
1

我看到這個帖子裏面是複製.db文件包括我與我的應用程序並將其複製到相應的目錄中有所幫助:Where would you place your SQLite database file in an iPhone app?將文件複製到應用程序文檔目錄,差錯控制

請問,做工作的方法提供任何類型的失敗或錯誤處理?就像用戶認爲應用程序被卡住了/ b,轉移花費的時間太長,並決定強制退出應用程序?

方法:

// Creates a writable copy of the bundled default database in the application Documents directory. 
- (void)createEditableCopyOfDatabaseIfNeeded { 
    // First, test for existence. 
    BOOL success; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"bookdb.sql"]; 
    success = [fileManager fileExistsAtPath:writableDBPath]; 
    if (success) 
     return; 
    // The writable database does not exist, so copy the default to the appropriate location. 
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bookdb.sql"]; 
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 
    if (!success) { 
     NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
    } 
} 

回答

1

不,它不會做任何事情,如果有相同名稱的現有文件。

因此,如果複製因爲某種原因在中間被中斷,那麼下次運行它會認爲文件已經存在。

也許您應該首先使用臨時文件進行復制,然後如果完成,請將其重命名爲正確的名稱。

BOOL success; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error; 

// Getting the writable path of the file 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"bookdb.sql"]; 

// Checking if the file exists at the writable path 
success = [fileManager fileExistsAtPath:writableDBPath]; 
if (success) 
    return; 

// Incase the file does not exist 
// Getting the temporary path of the file 
NSString *tempDBPath = [documentsDirectory stringByAppendingPathComponent:@"bookdb_temp.sql"]; 

// Just making sure no temporary file exists from a previous operation 
success = [fileManager fileExistsAtPath:tempDBPath]; 
if (success) 
{ 
    success=[fileManager removeItemAtPath:tempDBPath error:&error]; 
    NSAssert1(success, @"Failed to delete temp database file with message '%@'.", [error localizedDescription]); 
} 

// Getting the default path of the file 
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bookdb.sql"]; 

// Copying the file from the default path to the temporary 
NSLog(@"Starting to copy"); 
success = [fileManager copyItemAtPath:defaultDBPath toPath:tempDBPath error:&error]; 
NSAssert1(success, @"Failed to copy database file with message '%@'.", [error localizedDescription]); 
NSLog(@"Finished copying"); 

// Renaming the temporary file which wouldn't take time but to ensure the copying has finished successfully 
success= [fileManager moveItemAtPath:tempDBPath toPath:writableDBPath error:&error]; 
NSAssert1(success, @"Failed to rename temp database file with message '%@'.", [error localizedDescription]); 
+0

你能提供一些關於如何做到這一點的代碼?我對整個重命名文件都陌生,訪問編碼類型的目錄中的文檔。我主要做了UI的東西。謝謝。 – Crystal 2012-02-16 17:16:47

+1

用代碼更新了答案,但您應該至少測試一次,只需啓動它並在複製並關閉應用程序時關閉該應用程序,然後查看它是否處理自身,則應重新開始複製​​並修復情況。 – iDifferent 2012-02-17 18:15:37

相關問題