开发者

CoreData updated sqlite database

I'm working on my first application using coredata. Everything works perfectly. When I debug the app on my device (without it already being installed on the device), then quit the app, manually modify the sqlite dbase then debug the app again, it appears to be using the old version of the database. If the two sqlite files have the same name, is there a way to te开发者_如何学运维ll it to just replace what's there?

Here's a snippet from my code:

NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"mydata.sqlite"];

    /*
     Set up the store.
     For the sake of illustration, provide a pre-populated default store.
     */
     NSFileManager *fileManager = [NSFileManager defaultManager];
     // If the expected store doesn't exist, copy the default store.
     if (![fileManager fileExistsAtPath:storePath]) {
        NSLog(@"copying default from sqlite");
        NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"mydata" ofType:@"sqlite"];
         if (defaultStorePath) {
            [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
         }
     }


    NSURL *storeUrl = [NSURL fileURLWithPath:storePath];

As you can see, if it doesn't find the database initially it loads the sqlite file from the app. Other than changing the name of the sqlite file or using a setting, is there a way to say flush whatever you have if you have anything, and start over from scratch?

I'm concern about users buying the app then when I release an update, they are still seeing data from the old version of the database.

Thanks, Howie


You can delete the old database, and copy the one that ships with your update the same way you did it originally.

NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"Test.sqlite"];
NSURL *storeURL = [NSURL fileURLWithPath:storePath];
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

You just need some way to determine if the the database is out-dated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜