{"id":57,"date":"2016-09-24T19:02:30","date_gmt":"2016-09-24T09:02:30","guid":{"rendered":"http:\/\/www.codenkeg.com\/blog\/?p=57"},"modified":"2016-09-25T01:09:32","modified_gmt":"2016-09-24T15:09:32","slug":"how-to-do-a-lightweight-core-data-migration-in-swift","status":"publish","type":"post","link":"https:\/\/www.codenkeg.com\/blog\/2016\/09\/24\/how-to-do-a-lightweight-core-data-migration-in-swift\/","title":{"rendered":"How to do a lightweight Core Data migration in Swift"},"content":{"rendered":"<p>Here&#8217;s a stack overflow <a href=\"http:\/\/stackoverflow.com\/questions\/34912733\/how-to-do-a-lightweight-coredata-migration-in-swift\">post<\/a> on the topic.<br \/>\nDefine <code>migrationOptions<\/code> to be used in <code>persistentStoreCoordinator<\/code><\/p>\n<pre class=\"lang:swift\" title=\"migrationOptions\">let migrationOptions = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true]<\/pre>\n<p>Inclue options in:<\/p>\n<pre class=\"lang:swift\" title=\"coordinator\">try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: migrationOptions)\r\n<\/pre>\n<p>It should end up like this:<\/p>\n<pre class=\"lang:swift mark:6,9\" title=\"persistentStoreCoordinator\">\r\n    lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {\r\n        \/\/ The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.\r\n        \/\/ Create the coordinator and store\r\n        let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)\r\n        let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent(\"SingleViewCoreData.sqlite\")\r\n        let migrationOptions = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true] \/\/ Migration options added to standard Core Data stack\r\n        var failureReason = \"There was an error creating or loading the application's saved data.\"\r\n        do {\r\n            try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: migrationOptions)\r\n        } catch {\r\n            \/\/ Report any error we got.\r\n            var dict = [String: AnyObject]()\r\n            dict[NSLocalizedDescriptionKey] = \"Failed to initialize the application's saved data\"\r\n            dict[NSLocalizedFailureReasonErrorKey] = failureReason\r\n\r\n            dict[NSUnderlyingErrorKey] = error as NSError\r\n            let wrappedError = NSError(domain: \"YOUR_ERROR_DOMAIN\", code: 9999, userInfo: dict)\r\n            \/\/ Replace this with code to handle the error appropriately.\r\n            \/\/ abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.\r\n            NSLog(\"Unresolved error \\(wrappedError), \\(wrappedError.userInfo)\")\r\n            abort()\r\n        }\r\n        \r\n        return coordinator\r\n    }()\r\n<\/pre>\n<p>Similarly in Objective C. Thanks <a href=\"https:\/\/twitter.com\/rockvotes\">@rockvotes<\/a><\/p>\n<pre class=\"lang:objc\" title=\"migrationOptions\">NSDictionary *migrationOptions = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a stack overflow post on the topic. Define migrationOptions to be used in persistentStoreCoordinator let migrationOptions = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true] Inclue options in: try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: migrationOptions) It should end up like this: lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = { \/\/ The persistent store coordinator for the application. This implementation &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.codenkeg.com\/blog\/2016\/09\/24\/how-to-do-a-lightweight-core-data-migration-in-swift\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to do a lightweight Core Data migration in Swift&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_newsletter_tier_id":0,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[14,15,5],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7VdP6-V","_links":{"self":[{"href":"https:\/\/www.codenkeg.com\/blog\/wp-json\/wp\/v2\/posts\/57"}],"collection":[{"href":"https:\/\/www.codenkeg.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codenkeg.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codenkeg.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codenkeg.com\/blog\/wp-json\/wp\/v2\/comments?post=57"}],"version-history":[{"count":8,"href":"https:\/\/www.codenkeg.com\/blog\/wp-json\/wp\/v2\/posts\/57\/revisions"}],"predecessor-version":[{"id":65,"href":"https:\/\/www.codenkeg.com\/blog\/wp-json\/wp\/v2\/posts\/57\/revisions\/65"}],"wp:attachment":[{"href":"https:\/\/www.codenkeg.com\/blog\/wp-json\/wp\/v2\/media?parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codenkeg.com\/blog\/wp-json\/wp\/v2\/categories?post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codenkeg.com\/blog\/wp-json\/wp\/v2\/tags?post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}