How to migrate new data model in a CoreData project? - iphone

I have a simple problem, I have an app based on CoreData and I need to change the structure a little. How can I migrate the old structure into the new one. Just adding one attribute will chrash the whole app.
Thanks

Have a look at the Apple Docs
http://developer.apple.com/iPhone/library/documentation/Cocoa/Conceptual/CoreDataVersioning/Introduction/Introduction.html

Once you have the basics of Core Data down, you can implement versioning. Until then, if you're just testing things out, you can always delete the app from the Simulator or device. Reinstalling the app will create a fresh data store (usually).

When you finally do get to the point where you want to version, you add a new one by clicking on your core data model file (YourApp.xcdatamodel). Then pull down the 'Design' menu, hover on 'Data Model', and click 'Add Model Version'.
Don't forget to change your model extension from 'mom' to 'momd' (wherever you're loading it, probably in your app delegate), and when you have your new version in hand, go back to that menu and click 'Set Current Version'.

Related

Core Data Entities multiple build configurations

I have 3 build configurations, Debug, Release and Development. For Development I have created a new Scheme with a new bundleID, Product name etc so I can see which one is DEV and DEBUG on my phone.
I am using Core Data and for the DEBUG with the original bundleID everything works fine, but when I switch to use the Development one, I get an error when I try to create a new entity with name:
NSEntityDescription.insertNewObject(forEntityName: "MyEntity", into: managedContext)
The error I get:
Could not cast value of type 'NSManagedObject_MyEntity_' (0x600002d62b20) to 'AppName_DEV.MyEntity' (0x102bf9368).
It seems like it is trying to get the MyEntity class from my "AppName DEV" scheme not the "AppName". I can't find any Build Settings where I could point it to use the same model?
Any ideas on how to use core data with multiple build configurations?
Ok, I found the problem. Since I was trying to revive a very old app originally written in swift 2, it occurred to me that I might double check how was the data model created. After hours of tinkering with the core data set up process, I changed 2 things that finally get it to work.
1.) In the .xcdatamodeld file, on the right side in the Data Model Inspector, make sure that you have the Module field EMPTY I had my AppName Inside, which was the same as the original target name.
2.) I added #objc(EntityName) to the NSManagedObject subclass I had.
Of course I cleaned the project, deleted the app from the simulator and installed it again.
If you have an issue like that, hope that whis will help you, good luck!

Preventing a CoreData crash for upgrading users

I built an app about a year and a half ago that I'm coming back to. It was the project I cut my Swift teeth on and obviously a lot has changed since then, both in the language and my Swift abilities.
Yesterday for the first time, I updated my single CoreData model to add an optional string attribute. I did the file generation bit and made sure in the inspector column the new data model is appropriately selected.
On my Simulator and testing devices, I need to delete the old version of the app to install the new version or I get a crash. I assume that's just part of the development environment process. How can I ensure that upgrading users won't have to delete and reinstall when they do a simple update from the App Store? I assume that Xcode/CoreData/Apple have this handled with some internal scripts or processes that are invisible to the user, "it just works." But I wanted to post this here to understand if there's anything additional I need to do to ensure a smooth transition from v1 to v1.1 for the user.
All I did was an an optional string column, as I mentioned. I assume that all existing user data will be migrated over to the new schema with the new field being nil.
Any thoughts here would be very welcomed and appreciated. Thanks!
If your app is crashing in the Simulator when upgrading, your users will have crashes too.
To avoid this, you need to make sure you follow these steps:
Make sure you do NOT change the original version of your data model in any way.
In Xcode, select your xcdatamodel file, then from the menu choose Editor > Add Model Version...
Xcode will suggest a new version name, based on the current model. Make a mental note of the new version name, then click Finish.
Select the xcdatamodel file again, go to File inspector and under Model Version, select the new version name to make this your current version.
In Project Navigator, select the new version of the xcdatamodel. Add your attribute.
It's important to follow these steps in this order. If you add your attribute before creating the new model or making it your current version, you will have crashes.
EDIT: This will only work if you enable lightweight migrations. This is a code snippet of how to do this:
let options = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true]
do {
//coordinator is an NSPersistentStoreCoordinator
try coordinator!.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: options
} catch var error as NSError {
// handle error however you want here...
abort()
}
In addition to #Mike Taverne's answer, I think it a better if I post some screenshots to illustrate the option
for Xcode 12.3
Choose from the main menu Editor -> Add Model Version
To add mark the New Model as the current model with a green checkmark
Follow the below image

Core Data - Changing attributes type

I have a project and I am using core data.
I have some entities, with attributes. When I started the project I choose some atributes and now I want to change their types (Int to String for example). ANd so I did it.
THe thing is, I am getting errors...
I checked the code, I think every thing is ok.
I even deleted the entire entity and made a new one with some name but it doesn't work.
How can I change it with success?
You can use code data migration for this by creating new version..
To change data types you need to create a new version of the database, you can not just simply modifie it because that way your users would have to delete and redownload your app every time you change something.
Here you can read how to do this.
The simulator or device you are running the app on still 'remembers' the old type and data. Simply hold down on the app and press the 'X' to delete it. When you press play in XCode it will reinstall the app with the new data types.

Core Data Error

I am creating an application using Core Data. But When I run my app it crashes. It shows me this reason:
reason = "The model used to open the store is incompatible with the one used to create the store";
What should I do now?
I think you need to refer this link and refer to answer given by Grouchal in the link below:
What do I have to do to get Core Data to automatically migrate models?
If you need any further help please do let me know.
Hope this helps you.
It means that you created a model, run the app, modified the model (by adding or removing managed objects, renaming object properties and so on...) and reopened the app without migrating the data first. I still have to try this by myself but, you should definitely take a look here: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreDataVersioning/Introduction/Introduction.html
You can just delete the app (icon) from the simulator or device. Then, rebuild and run.
As dave explained, you have modified the core data structure (the store). If you're using the standard template, in the new rebuild a new model (NSManagedObjectModel) will be created that is compatible with your newly modified store.
You will probably be making many changes to the core data structure (adding or deleting an entity or some property or relationship), and, each time you do, you can just delete the app from the simulator or device.

Cant find Manage Object Class in xCode for iPhone

I am trying to generate classes for my .xcdatamodel entities, but am not given the Manage Object Class option when I click on an entity and select File>New File. I've looked under both the iPhone and Mac OS X catagories, but it is not listed anywhere.
Here are the steps I am taking,
I create a new project being sure to check the "Use Core Data" checkbox. Once the project is created, I open .xcdatamodel, create an entity, and add a few attributes. Next, I click on the entity and choose File>New File, but all I see are only Objective-C, Objective-c test case class, and UIVewController subclass.
Am I missing a step?
Thanks in advance
Take a look at the Core Data Tutorial for iPhone OS and look at this section: Custom Managed Object Subclass. If you still aren't seeing the Core Data related items for the iPhone OS check the version of the SDK you are targeting. I believe Core Data has only been available since SDK 3.
Had the same problem. Restarting x-code helped me.