开发者

Override Coredata DB With A New Structure DB

Ok guys i hope you help me with this. i really need your help.

I have an application on the app store which uses seed database (check if the db exists in the "persistentStoreCoordinator" method in the app delegate if doesn't exist copy the seed DB).

The problem is that i change the DB structure and of course the seed db structure as well. and published the update to the app store. when the u开发者_开发百科sers update the application and open it, the app crashes. because the code that checks if the db exists in the documents folder can find the db so it will use the old db in the app that needs the new db structure.

I wonder how i can overcome this problem. the issue is that i cannot just replace the existing db because then i will not be able to copy the seed db.

Please help me :)


Why are you changing the DB structure, and not the data model? Core Data isn't a database. It's designed to abstract the database away so you don't have to think about it. If you need to manipulate the database directly you should be using SQLite.

Core Data provides both automatic and manual model migration to allow you to change your data model and have Core Data take care of the DB. Seriously, the entire point of Core Data is you never have to deal with the database directly.


Normally what you would do is migrate the existing DB to the new database model. If the changes are simple you can most likely get away with what is called a lightweight migration. Have a look at Introduction to Core Data Model Versioning and Data Migration Programming Guide


Ok i managed to solve the issue by checking if the application is opening for the first time. if it is true, then i will delete the old db and then put the seed db (with the new model).

i used this code:

if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firsttimer"] ) {
    if ([fileManager fileExistsAtPath:storePath]) {
        [fileManager removeItemAtPath:storePath error:nil];
    }
    [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"firsttimer"];
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜