我看到这篇文章有助于复制我在应用程序中包含的.db文件并将其复制到相应的目录:Where would you place your SQLite database file in an iPhone app?
执行工作的方法是否提供任何类型的故障或错误处理?就像用户认为应用程序被卡住b / c一样,转移花了太长时间并决定强制退出应用程序?
方法:
// 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]);
}
}
答案 0 :(得分: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]);