0

I'm working on an old iOS app which has in app database i.e. pre populated database is provided to the app so that app can read(only read), now in updated version only information in table is updated(tables are same). when i test new data base i have to delete app from device to get new data otherwise i get old data.

So my question is that how to migrate from old data to new data if user update the application from App Store?

I've gone through this and this SO post but these post are about alter tables but i want to update only my records.

Edit:- If i change database file name while searching in document directory with new database file name, will it do the trick?Here is my code-

- (void) copyDatabase { 

//Using NSFileManager we can perform many file system operations. 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error; 
dbPath = [self getDBPath]; 
BOOL success = [fileManager fileExistsAtPath:dbPath]; 

if(!success) { 

NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"mydb1.1"]; 
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error]; 

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

- (NSString *) getDBPath { 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); 
NSString *documentsDir = [paths objectAtIndex:0]; 
return [documentsDir stringByAppendingPathComponent:@"mydb1.1"]; 
}

"mydb1.1" is the new file name.

Community
  • 1
  • 1
Blios
  • 719
  • 1
  • 16
  • 30
  • What's the issue then? Your app knows what version it is and the database knows what version it is and if they differ you do an upgrade. If you don't need to alter tables then your task is even easier. – trojanfoe Sep 22 '15 at 06:45
  • i just replaced my old database file with new one, should i keep both with different name? – Blios Sep 22 '15 at 06:56
  • That is one way to do it. Don't forget to keep track of the schema version currently being used within the database itself. I would create new, migrate current -> new, rename current -> old, rename new -> current. – trojanfoe Sep 22 '15 at 06:57
  • ok, what if i put my new db file with different name, lets say "mydb1.1" and give this name while searching in app bundle? like - [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"mydb1.1"] – Blios Sep 22 '15 at 07:03
  • Searching app bundle? What's the app bundle got to do with it? – trojanfoe Sep 22 '15 at 07:03
  • I'm looking for db file in both bundle and document directory, so give new name will do the trick ? – Blios Sep 22 '15 at 07:06
  • That's not how you do it, is it. You move the database from the app bundle into the document folder if it doesn't exist there and always use the same file in the document folder. – trojanfoe Sep 22 '15 at 07:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90291/discussion-between-blios-and-trojanfoe). – Blios Sep 22 '15 at 07:10

0 Answers0