I setuped CoreData using Magical Records with line
[MagicalRecord setupCoreDataStackWithStoreNamed:#"MyModel"];
Now in phase 2 of the app I'm doing
[MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:#"MyModel 2"];
but what happens is that the data gets cleared. Everything starts from scratch :( I'm in deep nuisance please help. Anybody?
When you make changes to an existing core data model you have to add a new version.
This doesn't mean that your data base name has changed, all thats happening is that you are telling Core Data that a new version has been added and thats where the auto migration magic can begin. Thats why you can open your .xcdatamodeld in finder, show package contents, and you will see your two versions.
So continue initialising Magical Record with the original model name: [MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:#"MyModel"];
You should have a green tick next to MyModel.xcdatamodeld 2 in your file inspector:)
You also need to be aware that MagicalRecord will delete the model in Debug mode if it cannot perform migrations so please read the lightweight migration guide on how to setup valid model changes for lightweight migration
Related
I have a model Version. The entity passPhrase of Version needs to be migrated to New entity passPhrase in Home model.
But the Home model is introduced in new version of database. It was not present in old xcdatamodel. How can I migrate data from
Version.passPhrase to Home.passPhrase
And the golden rule of CoreData migration is - avoid custom migration at any cost. :)
So the way to do this to use lightweight migration to add your new entity, and then just use a one off migration script to move your data over.
You can remove the passPhrase property from your Version class definition but you need to leave it in the model for now so you can still access the old data. This can be cleaned up at some time in the future when you are confident all of your users have upgraded to the newer version, or just leave it there forever.
To access the existing value during your copy just use [version valueForKey:#"passPhrase"] and then once copied clear it out the same way [version setValue:nil forKey:#"passPhrase"]
In core data, Changing Entity / Model mapping does not comes under light weight migration. You have to handle it manually by subclassing NSMigrationManager and implementing custom NSEntityMigrationPolicy.
Apple provides little documentation on the subject.Please check the Custom Core data migration with detailed example. Hope this will help.
I need to change my attribute name of entity in coredata ios. I want lightweight migration of it without crash. I have tried lot by referring migration tutorial but app directly goes crash.
Try Out with this link you can find demo here (http://www.raywenderlich.com/86136/lightweight-migrations-core-data-tutorial)
Here is the list of step you have to follow.
After this make required changes into your new model(Example 2).
Select your current model version -
In our existing app, we have many different version of xcdatamodel:
+ TheApp.xcdatamodel
TheApp.1.0.xcdatamodel
TheApp.1.1.xcdatamodel
TheApp.2.0.xcdatamodel
...
We know that the traditional way of doing database migration testing is what have been proposed in this question: How to Test Core Data Migration With an App Already in the App Store? In short, it works like the following:
install a old version of the app;
create some data in the old version of the app;
install the new version on top it;
see if everything is migrated properly.
We have been using this migration testing method for all our previous version of the app. Our QAs will perform the above steps and then judge by themselves that whether migration is successful or not.
However, in the most recent upgrade, we have changed a lot in our data model. It doesn't sound like such a good idea to ask the QAs to remember what have been created in the old version of the app and know what have been missing or not during migration. Therefore, we would like to see whether it is possible to write unit testing for the database migration by the developers ourself.
So one of the first step is to generate test data. Notice that we can see all the previous version of the xcdatamodel from within our Xcode project, it seems that it is possible. In a nutshell, the question:
Is it possible to generate test data of previous version of xcdatamodel programmatically from within our current version of the app?
Please let me know what you think. Suggestions are also acceptable.
This is how we do it: we need to first get an URL to any model you would like to work with, and then create a managedObjectModel from it.
let oldModelUrl = NSBundle.mainBundle().URLForResource("CoreDataExample.momd/CoreDataExample",
withExtension: "mom")!
let oldManagedObjectModel = NSManagedObjectModel.init(contentsOfURL: oldModelUrl)
Referece: https://medium.com/#yzhong.cs/1d9f941b3168.
At the early stages of development, where data retention is no issue and experimental changes to the schema are expected to be numerous, is there a way to avoid migration? Namely, just to dump the database and start over each time.
Everything I try leads to the error: "The model used to open the store is incompatible with the one used to create the store".
Lightweight migration looks do-able, but I'll end up lots of unwanted versions and messy code. I'd also like to experiment with relationships (no wise cracks), but understand I'd need to commit to a heavyweight migration afterwards!
If the solution is to check for the existence of the original database using NSFileManager and delete/remove it if found, could someone please show the swift code with an example name of the original database, and how it can be effectively removed.
I've been trying several suggested solutions, such as dragging a copy of the .xcdatamodeld file onto the desktop, deleting the file in xcode and bringing it back in, but I keep getting the error that the model and store are not the same.
Answered in one line in first comment, but appreciated in reverse proportion.
I received the source for an application from a guy to make a few changes in the app. The data model version was, say, App3. I had to add about 3-4 more data model versions in the process. So the active model version when I sent him was App7
I built the project (it was running properly on my mac)
I sent him the zip and when he ran, he got the error "Can't find model for source store"
I had him create the latest data model again, based on the data model App3 and make it the same as App7 and set it as the current version.
He still has the error and I can't figure out why? Are the intermediate data model version causing issues? Is it required to delete the sqlite file in the application support->iphone simulator folder? I am all over the place with this. Please help!
You need to have both model files and have some sort of versioning in place. The new model that you made will no longer work with his data file unless you specify to Core Data that you want it to update the data.
Versioning isn't that easy, but if your changes are small enough, you might be able to get away with automatic versioning. Read the guide and see:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/Introduction.html
In my understanding, it will. But really that is not the thing to do if ultimately the versioning is going to happen on existing live apps too.
For the light-weight migration to work, you need to send him all the intermediate versions of the model as well since the migration takes place stage by stage through all versions.
And you have to make sure that you have made no modification (even accidentally) to the base version App3.