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 -
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.
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.
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
I'm using xcode 5 to develop an app for iPhone, I use lightweight migration to update the Core Data schema.
I create a User entity in model version 1, User has a property called nameA.
In model version 2, I rename User's nameA to nameB, I set nameB's renaming id to nameA. This step is successful, previous value of nameA can be found in nameB.
Then I create model version 3 to add another entity called House, and I found in version 3, User's nameB still has its renaming id set to nameA
I think in model version 3, User's nameB should not has a renaming id, because model version 3 is based on version 2, and in version 3 I did not change anything of User entity
So should I delete the renaming id of User's nameB? or just leave it there?
Anyone know how?
You should leave it there. Your users may not have upgraded to version 2 of your core data schema. When those users upgrade from v1 to v3 they will need to know how to migrate from nameA to nameB
If you use lightweight migration you should not be editing the migration scheme at all. The whole point of lightweight migration is that you do not have to care about renaming ids and other such things - it's automatic!
Here is your workflow:
create a new version
activate it
make your model changes
for subsequent versions, repeat the above
That's all. The changes you describe (they are all lightweight compatible) should just work.
I have an existing iPad application to which I've just added core data versioning. I've been through the documentation and followed the steps detailed. Now after choosing my new model as the current versioned model and trying to run it on my dev device from xCode I get the following error:
2012-03-28 07:35:42.137 DocsOnTap[2603:707] CoreData: error: (1) I/O error for database at /var/mobile/Applications/06EECF01-3598-4513-8A3A-BE4FD49EEBF6/Documents/.DocsOnTap.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3. SQLite error code:1, 'table Z_2TAG already exists'
The only change that I made to my model was to add in a single new entity. I have a table named Tag in my model - that appears to be what the error is referring to.
If I revert my current versioned model back to the previous model version then I can run my app on my dev device from Xcode without error.
I have read that there can be problems trying to use core data migration on dev devices. However I just want to test the process to be sure that when we update our app in the Appstore the migration works as expected for our customers.
In my case the same error appeared due to Renaming ID which was set in the Data Model inspector for the Entity. After I removed the Renaming ID the problem'd gone.
Well this was an obscure error. The entity that I was adding was named AppKeys - this must be the name of an entity used internally by core data or SQL Lite. I went back to scratch and found that I could add and migrate other attributes and entities without any problems. However if I tried once again to add my entity named AppKeys then I got the same error saying that 'table Z_2TAG already exists'. So the resolution to my problem is to choose another entity name. It is a pity that this is not documented somewhere obvious - or that the error was not more helpful. Anyway hopefully this might just help someone else one day.