2012-02-16 61 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

相关问题