【问题标题】:Replace File in Documents Directory替换文档目录中的文件
【发布时间】:2016-09-19 18:01:24
【问题描述】:

我有一种情况,我为我的应用程序的下一个版本对 SQLite 数据库进行了更改,由于对 SQLite 数据库的更改,我还对从中读取的一些代码进行了更改,主要是添加列到查询方法。

由于下次用户更新应用程序时所做的更改,他们必须拥有新的数据库才能使其工作。在我的应用程序的早期版本中,我有一个名为 File.sqlite 的数据库,当用户下载应用程序时,该文件会保存到他们的文档文件夹中。现在我用一个新文件构建了应用程序,但同名 File.sqlite 有更多列等,但是当我更新应用程序(不是全新安装)时,新文件不会替换旧文件。

我如何确保当用户更新应用程序时文件将被替换/更新?

截至目前,我知道我可以使用以下方法来获取文档目录中旧文件的路径:

//Reference the path to the documents directory.
NSString *documentDir =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//Get the path of old file.
NSString *filePath = [documentDir stringByAppendingPathComponent:@"File.sqlite"];

新文件是否必须具有不同的名称,而我只是在代码中引用新名称?这对我来说似乎很愚蠢,我必须能够复制旧版本并使用新版本?

另外,如果您知道更好的方法,我什至不确定这是否是做我想做的事情的最佳方法。

【问题讨论】:

  • 你的答案肯定在these search results
  • @rmaddy 如果我能找到解决方案,我不会问我的问题,我已经尝试查看这些解决方案。
  • 我有一个应用程序,其中我不时遇到类似的情况。我所做的是在NSUserDefaults 中使用一个标志来跟踪当前正在使用哪个版本的数据文件。当需要一个新文件时,我更改当前文件的文件名(通过附加OLDddMMYYYY),然后复制新文件。效果很好。
  • 你在使用核心数据吗?

标签: ios objective-c nsdocumentdirectory


【解决方案1】:

您可以检查您的应用程序是全新安装还是来自 AppDelegate 的更新。但是,您似乎没有将应用程序版本保存在 NSUserDefaults 中,这对您来说有点棘手。您将新版本的 File.sqlite 保存在 Resource 中并将其复制到文档目录。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
    NSString* curVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
    NSString* lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"LastVersion"];

    if ( lastVersion == nil ) 
    {
        //App Installed for the first time. In your case it might be an update because you do not store lastVersion value.

        //Reference the path to the documents directory.
        NSString *documentDir =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
        //Get the path of old file.
        NSString *filePath = [documentDir stringByAppendingPathComponent:@"File.sqlite"];

       if([[NSFileManager defaultManager] fileExistsAtPath: filePath])
       {
          [fileManager removeItemAtPath:filePath error:&error] //Delete old file
       }
       //Copy New File.sqlite from Resource Bundle.
       NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"File" ofType:@"sqlite"];
       [fileManager copyItemAtPath:resourcePath toPath:filePath error:&error]; 
    } 
    else if (![lastVersion isEqual: curVersion]) 
    {
      // App is updated. For future Versions you can Update files from here! 
    } 

    [[NSUserDefaults standardUserDefaults] setObject:curVersion forKey:@"LastVersion"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

希望这会有所帮助!

【讨论】:

    【解决方案2】:
    NSArray *pathsToDocuments = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [pathsToDocuments objectAtIndex: 0];
    NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"File.sqlite"]; //File.sqlite old database
    
    NSString *pathStr = [documentsDirectory stringByAppendingPathComponent:@"File1.sqlite"]; // New database file name
    FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
    [database open];
    FMDatabase *database1 = [FMDatabase databaseWithPath:pathStr];
    [database1 open];
    
    NSString *str = @"select * from table_name"; // old database fire query for use data value fetch
    
    FMResultSet *res=[database executeQuery:str];
    
    //   Your old database value updated to update new database.
    while ([res next]) {
        NSString *query= // Write update query for update column value
        [database1 executeUpdate:query];
    }
    [database1 close];
    [database close];
    
    // remove old database file 
    if ([[NSFileManager defaultManager] isReadableFileAtPath: dbPath])
    {
        if ([[NSFileManager defaultManager] removeItemAtPath:dbPath error: NULL] != YES)
            NSAssert2(0, @"Fail to copy database from %@ in %@", dbPath, pathStr);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-19
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 1970-01-01
      • 2020-12-06
      • 1970-01-01
      相关资源
      最近更新 更多