【问题标题】:Copying files to Applications Documents Directory, error control将文件复制到应用程序文档目录,错误控制
【发布时间】:2012-02-18 02:14:15
【问题描述】:

我看到这篇文章有助于复制我的应用程序中包含的 .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]);
    }
}

【问题讨论】:

    标签: iphone


    【解决方案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]); 
    

    【讨论】:

    • 你能提供一些关于如何做到这一点的代码吗?我对整个重命名文件有点陌生,在目录类型的编码中访问文档。我主要做UI的东西。谢谢。
    • 用代码更新了答案,但你应该至少测试一次,只需启动它并在复制时关闭应用程序并重新启动它,看看它是否自行处理,它应该重新启动复制并修复这种情况.
    猜你喜欢
    • 2012-08-05
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多