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