{"id":18040,"date":"2015-03-18T08:38:55","date_gmt":"2015-03-18T03:08:55","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=18040"},"modified":"2016-01-26T09:23:04","modified_gmt":"2016-01-26T03:53:04","slug":"coredata-migration-versioning-of-coredata","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/coredata-migration-versioning-of-coredata\/","title":{"rendered":"CoreData Migration (Versioning of CoreData)"},"content":{"rendered":"<p><strong>What is Migration ?<\/strong><\/p>\n<p>Migration is a way of telling Xcode how to transition the data from the old model to the new model .<\/p>\n<p><strong>Why migration required ?<\/strong><\/p>\n<p>When the model does not match the store, a migration is required. In order to perform a migration, Core Data (technically, an instance of NSMigrationManager) requires these things:<\/p>\n<ul>\n<li>The destination managed object model (the one with the changes)<\/li>\n<li>A managed object model that can open the existing store<\/li>\n<li>The ability to infer mapping between the two models (for a lightweight migration), or a manual mapping model<\/li>\n<li>Permission to attempt to perform an automatic migration (for a lightweight migration)<\/li>\n<\/ul>\n<p><\/b><\/p>\n<p>It is therefore absolutely essential that you never make changes to the managed object model for a released version of an app. That is, if your app is already in the App Store, <strong>don\u2019t change a single thing in that version of the managed object model<\/strong>.<\/p>\n<p><strong>Solution for the Crash while update an application from the app store:<\/strong><\/p>\n<p>Create a new version of the managed object model! This reminds me to mention some other best practices to adopt when working with Core Data:<\/p>\n<ul>\n<li>Create a new model version for every release version of an app<\/li>\n<li>Keep a copy of every release version of an app (you\u2019re already doing this, right?)<\/li>\n<li>Keep a copy of every release version backing SQLite store containing suitable test data<\/li>\n<\/ul>\n<p><\/b><\/p>\n<p><strong>Migrations can handle the following changes:<\/strong><\/p>\n<ul>\n<li>Adding or removing an entity, attribute, or relationship<\/li>\n<li>Making an attribute non-optional with a default value<\/li>\n<li>Making a non-optional attribute optional<\/li>\n<li>Renaming an entity or attribute using a renaming identifier<\/li>\n<\/ul>\n<p><\/b><\/p>\n<p><strong>Getting Started:<\/strong><\/p>\n<ul>\n<li>Create a new version of the managed object model  &#8211;  select .xcdatamodeld file in Xcode and then from the menu select <strong>Editor\\Add Model Version<\/strong> <\/li>\n<p><\/b><br \/>\n<\/b><\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Screen-Shot-2015-03-15-at-9.10.59-PM.png\"><img decoding=\"async\" loading=\"lazy\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Screen-Shot-2015-03-15-at-9.10.59-PM.png\" alt=\"Screen Shot 2015-03-15 at 9.10.59 PM\" width=\"1280\" height=\"800\" class=\"alignnone size-full\" \/><br \/>\n<\/b><br \/>\n<\/b><\/p>\n<li>Set version name and select model based on which you want to create new model.<\/li>\n<li>Switch to latest version of model \u2013 select  .xcdatamodeld file from Project Navigator and go to FILe Inspector and select latest core data model<\/li>\n<p><\/b><br \/>\n<\/b><\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Screen-Shot-2015-03-15-at-9.11.54-PM.png\"><img decoding=\"async\" loading=\"lazy\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Screen-Shot-2015-03-15-at-9.11.54-PM.png\" alt=\"Screen Shot 2015-03-15 at 9.11.54 PM\" width=\"1280\" height=\"800\" class=\"alignnone size-full\" \/><br \/>\n<\/b><br \/>\n<\/b><\/p>\n<li>Now you will see a little green circle with white checkmark badge on latest .xcdatamodel in the Project Navigator.<\/li>\n<p><\/b><br \/>\n<\/b><\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Screen-Shot-2015-03-15-at-9.12.01-PM.png\"><img decoding=\"async\" loading=\"lazy\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Screen-Shot-2015-03-15-at-9.12.01-PM.png\" alt=\"Screen Shot 2015-03-15 at 9.12.01 PM\" width=\"1280\" height=\"800\" class=\"alignnone size-full\" \/><br \/>\n<\/b><br \/>\n<\/b><\/p>\n<li>Add permissions to attempt to perform an automatic migration<\/li>\n<\/ul>\n<p><\/b><\/p>\n<p>Open .m file containing core data related code and add the following code immediately after<br \/>\n<code>persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];<\/code><br \/>\n<\/b><br \/>\n<\/b><\/p>\n<p>[code language=&#8221;objc&#8221;]<br \/>\nNSDictionary *options = @{<br \/>\nNSMigratePersistentStoresAutomaticallyOption : @YES,<br \/>\nNSInferMappingModelAutomaticallyOption : @YES<br \/>\n};<br \/>\n[\/code]<br \/>\n<\/b><br \/>\n<\/b><\/p>\n<p>Change the next line to pass the <strong>options<\/strong> dictionary you just created to<br \/>\n<code>addPersistentStoreWithType:configuration:URL:options:error:<\/code><br \/>\n<\/b><br \/>\n<\/b><\/p>\n<p>[code language=&#8221;objc&#8221;]<br \/>\nif (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType<br \/>\n         configuration:nil<br \/>\n         URL:storeURL options:options error:&amp;error]) {<br \/>\n[\/code]<br \/>\n<\/b><br \/>\n<\/b><\/p>\n<ul>\n<li>Now make changes to  an core data entity, attribute, or relationship whatever you want<\/li>\n<li>Delete CoreData NSManagedObect Subclass for entities in which you have made changes and create new file  (<strong>New File\/ CoreData\/  NSManagedObect<\/strong>)<\/li>\n<p><\/b><br \/>\n<\/b><\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Screen-Shot-2015-03-15-at-9.14.32-PM.png\"><img decoding=\"async\" loading=\"lazy\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Screen-Shot-2015-03-15-at-9.14.32-PM.png\" alt=\"Screen Shot 2015-03-15 at 9.14.32 PM\" width=\"1280\" height=\"800\" class=\"alignnone size-full\" \/><br \/>\n<\/b><br \/>\n<\/b><\/p>\n<li>Select latest Data Model and click Next<\/li>\n<p><\/b><br \/>\n<\/b><\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Screen-Shot-2015-03-15-at-9.15.30-PM.png\"><img decoding=\"async\" loading=\"lazy\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Screen-Shot-2015-03-15-at-9.15.30-PM.png\" alt=\"Screen Shot 2015-03-15 at 9.15.30 PM\" width=\"1280\" height=\"800\" class=\"alignnone size-full\" \/><br \/>\n<\/b><br \/>\n<\/b><\/p>\n<li>Select entities in which you have made changes and click next <\/li>\n<\/ul>\n<p><\/b><br \/>\n<\/b><\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Screen-Shot-2015-03-15-at-9.15.39-PM.png\"><img decoding=\"async\" loading=\"lazy\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Screen-Shot-2015-03-15-at-9.15.39-PM.png\" alt=\"Screen Shot 2015-03-15 at 9.15.39 PM\" width=\"1280\" height=\"800\" class=\"alignnone size-full\" \/><br \/>\n<\/b><br \/>\n<\/b><\/p>\n<p>Run your app and check out its working fine. Congratulations! You just successfully performed a  Core Data migration.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Migration ? Migration is a way of telling Xcode how to transition the data from the old model to the new model . Why migration required ? When the model does not match the store, a migration is required. In order to perform a migration, Core Data (technically, an instance of NSMigrationManager) requires [&hellip;]<\/p>\n","protected":false},"author":172,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":147},"categories":[1400,1772,1],"tags":[3011,3014,3015,1703,3016,3013,3012],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18040"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/172"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=18040"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18040\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=18040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=18040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=18040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}