views:

35

answers:

1

I want to introduce the ability for the my core-data app to download a NEW sqlite file and update its stored data. Note the data model is not changing.

At first I am not worrying about user changes to the stored data, and simply want to overwrite it.

  • I am finding the only way the stored data is updating is to change the name of the sqlite file.. ?
  • Is there a standatd way to merge, via core-data, 2 sqlite files? Thus keeping user modified data.
  • Can an update (downloaded sqlite file) be a subset of the shipped core data sqlite? Note the sqlite contains binary information.

I think this SO question falls short in answering these things.

Thanks for any guideance!

A: 

If you want to Update any data base file without modifying existing data base then you have to implement like this

- (void)applicationDidFinishLaunching:(UIApplication *)application {

[self createEditableCopyOfDatabaseIfNeeded];

}

- (void)createEditableCopyOfDatabaseIfNeeded {

    // First, test for existence - we don't want to wipe out a user's DB

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *documentDirectory = [self applicationDocumentsDirectory];

    NSString *writableDBPath = [documentDirectory stringByAppendingPathComponent:@"DataBaseName.sqlite"];

    BOOL dbexits = [fileManager fileExistsAtPath:writableDBPath];

    if (!dbexits) {

        // The writable database does not exist, so copy the default to the appropriate location.
        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"DataBaseName.sqlite"];

        NSError *error;
        BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
        if (!success) {

            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);

        }

    }

}
raaz