Robert Saunders Robert Saunders - 6 months ago 38
iOS Question

Replace File in Documents Directory

I have a situation where I have made changes to an SQLite database for the next release of my app, because of the changes to the SQLite database I have also made changes to some code that reads from it, primarily adding columns to the query method.

Because of the changes the next time the user updates the application they must have the new database for it to work. On the previous version of my app I have a database named File.sqlite, when the user downloads the application fresh that file gets saved to their documents folder. Now I have built the application with a new file but same name File.sqlite that has some more columns and such in it but when I update the application (not a fresh install) that new file does not replace the old.

How can I ensure that when the user updates the application the file will be replaced/updated?

As of right now I understand that I can use the following to get the path of the old file in documents directory:

//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"];


Does the new file have to have a different name and I just reference the new name in the code? That seems silly to me, I must be able to make a copy of the old and use the new version?

Also, I am not even sure if this is the best approach to do what I am trying to do so also if you know of a better way please advise.

Answer

You can check if your application is a fresh install or an update from AppDelegate. However, it seems you are not saving your App Version in NSUserDefaults, it will be a bit tricky for you. You save your new version of File.sqlite in Resource and copy it to document directory.

- (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:txtPath 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];
}

Hope this helps!

Comments