将文件复制到应用程序文档目录,进行错误控制 [英] Copying files to Applications Documents Directory, error control

查看:53
本文介绍了将文件复制到应用程序文档目录,进行错误控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了这篇文章,它有助于复制我的应用程序中包含的.db文件并将其复制到适当的目录中:

I saw this post which was helpful in copying a .db file I include with my app and copy it to the appropriate directory: Where would you place your SQLite database file in an iPhone app?

工作方法是否提供任何类型的故障或错误处理?就像是如果用户认为该应用卡在b/c上,则转移时间过长并决定强行退出该应用?

Does the method that does the work provide any type of failure or error handling? Like if the user thought the app was stuck b/c the transfer was taking too long and decided to force quit the app?

方法:

// 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]);
    }
}

推荐答案

否,如果存在同名文件,则不会执行任何操作.

No, It doesn't do anything if there's an existing file with the same name.

因此,如果复制由于某种原因在中间被中断,则下次运行时,它将认为文件已经存在.

So if the copying was interrupted for some reason in the middle, then next time it runs it'll think the file is already there.

可能您应该首先使用一个临时文件进行复制,然后完成复制,将其重命名为正确的名称.

Probably you should make the copying with a temporary file first and then if it finishes, rename it to the right name.

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]); 

这篇关于将文件复制到应用程序文档目录,进行错误控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆