{"id":3303,"date":"2023-06-10T17:59:11","date_gmt":"2023-06-10T21:59:11","guid":{"rendered":"https:\/\/michaelrowe01.com\/?p=3303"},"modified":"2023-06-10T17:59:13","modified_gmt":"2023-06-10T21:59:13","slug":"whats-new-in-core-data","status":"publish","type":"post","link":"https:\/\/michaelrowe01.com\/index.php\/blog\/whats-new-in-core-data\/","title":{"rendered":"What\u2019s new in Core Data"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"660\" height=\"371\" data-attachment-id=\"3304\" data-permalink=\"https:\/\/michaelrowe01.com\/index.php\/blog\/whats-new-in-core-data\/attachment\/%ef%a3%bfwwdc23-40\/\" data-orig-file=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-40.png?fit=1920%2C1080&amp;ssl=1\" data-orig-size=\"1920,1080\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"\uf8ffWWDC23-40\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-40.png?fit=660%2C371&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-40.png?resize=660%2C371&#038;ssl=1\" alt=\"\" class=\"wp-image-3304\" srcset=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-40.png?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-40.png?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-40.png?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-40.png?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-40.png?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-40.png?w=1320&amp;ssl=1 1320w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Composite attributes<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>New type of attribute that encapsulates complex and custom data types\n<ul class=\"wp-block-list\">\n<li>You can nest them and now create them in the Core Data model editor<\/li>\n\n\n\n<li>There is a demo of how to adopt these new Composite Attributes<\/li>\n\n\n\n<li>Watching the demo I can only think, how will SwiftData handle this, and if it does already, how much easier will it be<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Stage your migrations<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It is always preferable to do Lightweight migrations &#8211; it is built in and will happen automatically.&nbsp; Check out last year\u2019s Evolve your app\u2019s Schema &#8211; I captured my thoughts on that session <a href=\"https:\/\/michaelrowe01.com\/index.php\/blog\/wwdc-2022-day-four-the-nitty-gritty\/\">here<\/a> in the section on Core Data.<\/li>\n\n\n\n<li>When it is to complex, you can do a staged migration (this is new),\n<ul class=\"wp-block-list\">\n<li>For migration of non-conforming lightweight changes<\/li>\n\n\n\n<li>Simplify your app<\/li>\n\n\n\n<li>Provides some opportunities for your app to gain execution control during the migration to perform specific tasks\n<ul class=\"wp-block-list\">\n<li>You need to identify when Changes don\u2019t conform with light weight migration\n<ul class=\"wp-block-list\">\n<li>You can manually review the chains<\/li>\n\n\n\n<li>You can try to open the store with the lightweight option using NSMigratePersistentStoresAutomaticallyOption and NSInferMappingModelAutomaticallyOption set to true.<\/li>\n\n\n\n<li>You\u2019ll receive an hash error if they are not compatible<\/li>\n\n\n\n<li>Or you can use NSMappingModel.inferredMappingModel(forSourceModel:destinationModel:) which will either return the inferred model or nil if it cannot&nbsp;<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Decompose into a series of comforting migrations<\/li>\n\n\n\n<li>Describe the sequence of ordering using the new APIs<\/li>\n\n\n\n<li>Have Core Data execute an event loop to do the migration<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>The complex model change that is described may work for my migration, I will have to see if I can recreate this with multiple models<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Adding Staged migration allows you to mix and match (in sequence) your model migration from both lightweight and custom migration stages.\n<ul class=\"wp-block-list\">\n<li>You create a .willMigrateHandler to do custom work during migration, for each fetch entity you will copy in the data and relate them.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Defer your migrations<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If your lightweight migrations take too much time, which can frustrate your users.\n<ul class=\"wp-block-list\">\n<li>You can now do deferred migrations at a later date, for example dropping indices or deleting a column &#8211; you can defer this clean up by setting<\/li>\n\n\n\n<li>NSPersistentStoreDeferredLightweightMigrationOptionKey to True &#8211; whit is only available for Sqlight, but is available back to iOS 14<\/li>\n\n\n\n<li>You can check the store\u2019s metadata if that value is set and you can then process them by running NSPersistentStoreCoordinator.finishDeferredLightweightMigration()<\/li>\n\n\n\n<li>Consider using Background Task API to schedule this work<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>You can combine deferred and staged migrations<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Composite attributes Stage your migrations Defer your migrations<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_wp_convertkit_post_meta":{"form":"-1","landing_page":"0","tag":"0","restrict_content":"0"},"hide_page_title":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[2,3],"tags":[97,739,747,680],"class_list":["post-3303","post","type-post","status-publish","format-standard","hentry","category-blog","category-personal-softwareandit","tag-core-data","tag-day-6","tag-deferred-migrations","tag-wwdc23"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2aMa8-Rh","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3303","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/comments?post=3303"}],"version-history":[{"count":1,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3303\/revisions"}],"predecessor-version":[{"id":3305,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3303\/revisions\/3305"}],"wp:attachment":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/media?parent=3303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/categories?post=3303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/tags?post=3303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}