0

I am working on an application update. I have rewrote the application from the scratch and switched to magicalrecords for model controlling Heres my problem:

  1. I have created a new "newdatabase.xcdatamodel" file that contains an entity named "foo".
  2. At the first release of the app, i also had a "olddatabase.xcdatamodel" which also incloded an entity named "foo"
  3. When user installs the application from scratch theres no problem.
  4. If the user has a previous version of the app, his device has the "olddatabase.mom" file which also contains the old "foo" entity, so as you can imagine, the application crashed with the error:

Can't merge models with two different entities named 'foo''

So is it possible the detect and delete the old "olddatabase.mom" file before loading the new mom file?

i have tried the following code, and just as i thought, i have no permission on that folder :)

    NSString *path = [[NSBundle mainBundle]pathForResource:@"olddatabase" ofType:@"mom"];
if(path){
    NSError* error = nil;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:path error:&error];
    if(error)
        NSLog(@"Error: %@",[error description]);
}
NSLog(@"path = %@",path);

I am starting think about changing my new "foo" entity name to "foo2" or something else, but i really am obsessed with using proper names for things that i use so i'd be glad to learn if there is a way to do it.

Thanks for reading and (probably) responding :)

dreampowder
  • 1,644
  • 3
  • 25
  • 41

2 Answers2

2

OK I had exactly the same problem and this is how I solved it...

My new .xcdatamodelD had a different name. I think this was important in my case as I could specify Magic Record to use a specific momd instead of it searching through the bundle and complaining about:

Can't merge models with two different entities named 'foo''

So it's quite simple then. I just tell Magic Record to specifically use a new momd and then create the core data stack using the following setup calls...

[MagicalRecord setDefaultModelNamed:@"new_name.momd"];
[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:@"new_foosql.sqlite"];

If you want you could then nuke your old sql datastore, if thats what you want to do there are many answers on stack about resetting coredata.

Morgz
  • 574
  • 6
  • 15
1

What you described does not make senses:

A. You can't delete any thing from your bundle. It is read only.

B. When people update your new version, there should be no olddatabase.mom in your bundle. Most likely it is in the app's user document folder.

If I read your problem correctly, what you need is a core data migration. Try this.

Edit: FYI

After re-read OP's text, the B part above was not correct. But since OP had accepted this as answer, SO would not allow deleting of the answer.

Community
  • 1
  • 1
user523234
  • 14,323
  • 10
  • 62
  • 102
  • Thank you for the answer, the real problem is, i've created a new project, changed its revelant fields to make it the same as the old app. The new app doesnt have the corresponding xcdatamodel files, but those files exist on a user device whish has the old version of the app. – dreampowder Mar 27 '13 at 09:54
  • So if i understood this correct, what i have to do is adding the old .xcdatamodel file to the new project, add a new version and change the old "foo" entity in that xcdatamodel file as a new version instead of adding it to the new xcdatamodel file? – dreampowder Mar 27 '13 at 09:55
  • Thanks, solved by refactoring the new entity name (due to time limit). i have found that xcode has a wonderful refactoring tool for these kinds of situations. – dreampowder Mar 27 '13 at 11:23
  • I would love to have a good solution for this as I'm currently in exactly the same boat – Morgz Jun 14 '13 at 12:22