I've done a lot of changes to my core data model. In the past we used the simple automatic migration. However this will fail this time. Since I really don't care about the data being migrated I just want to delete the persistent store if auto migration fails and set it up again. Is this a valid way to go? Any thing I have to be careful ? Could this get my app rejected ?
1 Answers
There are some definite problems with doing that, and you need to be careful with it. This answer had some good advice from the NSManagedObjectContext's documentation
A context always has a “parent” persistent store coordinator which provides the model and dispatches requests to the various persistent stores containing the data. Without a coordinator, a context is not fully functional. The context’s coordinator provides the managed object model and handles persistency. All objects fetched from an external store are registered in a context together with a global identifier (an instance of NSManagedObjectID) that’s used to uniquely identify each object to the external store.
When faced with a similar situation in one of our apps, I opted to make a new persistent store, and deprecate the old one because our old store had been messed up on many of our devices by a previous bad migration. It ended up being a messier transition than I had hoped, but it did work.
The problems with your plan are not insurmountable, I'm just recommending caution. I liked Giao's advice of using NSManagedObjectContext's reset. When deleting and rebuilding, the persistent store coordinator could get confused. I worry because Apple seems to be doing so many things behind the scenes. I also worry because It seems like core data behaves differently on released apps than it does on our debug versions, especially in the upgrade process.
I think you are smart in recognizing that your automigrate is going to have trouble, and that you are looking for another path. In the recent past I've seen a group that really had to scramble for a month to deal with a failed data migration in their app.
-
Could you elaborate more on what kind of problems my approach has ? – kukudas May 02 '13 at 05:57
-
1@HalR: The question/answer you linked to is about emptying a persistent store that has already been opened. I have the feeling that the situation here is much simpler: If `addPersistentStoreWithType` fails due to migration problems, you can just remove the store file and call `addPersistentStoreWithType` again. – Martin R May 02 '13 at 10:58
-
Martin do you mean this solution? http://stackoverflow.com/questions/9548010/skipping-painful-migration-with-core-data-and-move-to-the-new-data-model my solution is kinda like this one. But i'm a bit scared that this could have some implications (app reject or somehow break my persistent store) – kukudas May 02 '13 at 11:38
-
@Martin R I was trying to express my concerns because of bad experiences I have had and observed in similar situations. I'd love to get more input from others if they've successfully pulled this off. – HalR May 02 '13 at 14:15