Copy Updated Database into device in ios sdk

In my app, i used the sqlite DB. I submitted the 1.0 version of the app to app store. But after that i made some changes into DB like Inserting some new rows.

Then submitted 1.1 version of the app. But i will not able to get updated DB because the old DB already exists in device.

How can i solve this?

If i delete the app & then install the new version of app, then i got the updated DB.

//My code for copy of DB is as follow:

- (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:@"xxx.sqlite"];
    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:@"xxx.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}

I called this method in appDidFinishLaunching method.

How can i get updated DB without deleting the app when new version is available

Answers


You can use the NSUserDefaults for doing this.

In your new version add this code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  if(![[NSUserDefaults standardUserDefaults] boolForKey:@"1.1"]])
  {
     [self removeDatabase];
     [self createEditableCopyOfDatabaseIfNeeded];
     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"1.1"];
  }
  //Other stuffs
}

Method for removing the old database file:

- (void)removeDatabase
{
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *removeDBPath = [documentsDirectory stringByAppendingPathComponent:@"xxx.sqlite"];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if(success)
    {
       [fileManager removeItemAtPath:writableDBPath error:&error]
    }
}

Need Your Help

How do I show indeterminate progress in the title bar on a tabhost?

android android-activity progress android-tabhost

I have a tabhost that contains three tabs showing three separate activities. One of those activities needs to show the indeterminate progress bar on the title bar.

Do Java object created natively need to be synchronized for access?

java ms-access object native synchronized

I am taking a look at Java's delivered code for Deflater. I notice that all methods accessing the ZStreamRef object created natively are synchronizing access to it. For example: