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]);
}
}
你能提供一些關於如何做到這一點的代碼?我對整個重命名文件都陌生,訪問編碼類型的目錄中的文檔。我主要做了UI的東西。謝謝。 – Crystal 2012-02-16 17:16:47
用代碼更新了答案,但您應該至少測試一次,只需啓動它並在複製並關閉應用程序時關閉該應用程序,然後查看它是否處理自身,則應重新開始複製並修復情況。 – iDifferent 2012-02-17 18:15:37