{"id":3224,"date":"2023-06-09T11:23:02","date_gmt":"2023-06-09T15:23:02","guid":{"rendered":"https:\/\/michaelrowe01.com\/?p=3224"},"modified":"2023-06-09T11:23:04","modified_gmt":"2023-06-09T15:23:04","slug":"model-your-schema-with-swiftdata","status":"publish","type":"post","link":"https:\/\/michaelrowe01.com\/index.php\/blog\/model-your-schema-with-swiftdata\/","title":{"rendered":"Model your Schema with SwiftData"},"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=\"3226\" data-permalink=\"https:\/\/michaelrowe01.com\/index.php\/blog\/model-your-schema-with-swiftdata\/attachment\/%ef%a3%bfwwdc23-27\/\" data-orig-file=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-27.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-27\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-27.png?fit=660%2C371&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-27.png?resize=660%2C371&#038;ssl=1\" alt=\"\" class=\"wp-image-3226\" srcset=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-27.png?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-27.png?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-27.png?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-27.png?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-27.png?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-27.png?w=1320&amp;ssl=1 1320w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/figure>\n\n\n\n<p>Utilize schema Macros<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Watch meet SwiftData and How to build an App with SwiftData<\/li>\n\n\n\n<li>Sample trip app is used with multiple relationships against the main @Model class<\/li>\n\n\n\n<li>Schema Macros allow you to customize the behavior of Schemas\n<ul class=\"wp-block-list\">\n<li>@Attribute(.unique) &#8211; allows for making a value is Unique &#8211; this will cause an update instead of a new items<\/li>\n\n\n\n<li>You can provide constrains on primitive value types, (Numeric, String, or UUID)<\/li>\n\n\n\n<li>You can also decorate a To-One relationship<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>To rename a variable &#8211; it will be seen as a new property so you can use @Attribute(originalName: \u201cold_name\u201d) var newName: String\n<ul class=\"wp-block-list\">\n<li>This will ensure a simple migration&nbsp;<\/li>\n\n\n\n<li>@Attribute also provides support for External Data, Transformable, Spotlight Integration and Has Modifier<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Working with Relationship &#8211; will auto setup implicit inverse relationships with default delete rule,&nbsp; You can add @RElationship(.cascade) to delete them verses, just nulling them out.<\/li>\n\n\n\n<li>To add non-persistent &#8211; just add @Transient and the value will be calculated at use time, must have a default logical<\/li>\n<\/ul>\n\n\n\n<p>Evolving schemas<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>To handle updates to your schemas between releases, you use VersionSchema to define distinct releases<\/li>\n\n\n\n<li>See SchemaMigrationPlan to do ordering of all updates needed to migrate the data\n<ul class=\"wp-block-list\">\n<li>Define each migration phase&nbsp; &#8211; either Lightweight (which requires no code), or Custom (which requires you to write code for things like-dedupe.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"660\" height=\"371\" data-attachment-id=\"3225\" data-permalink=\"https:\/\/michaelrowe01.com\/index.php\/blog\/model-your-schema-with-swiftdata\/attachment\/screenshot-2023-06-09-at-11-20-05-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/Screenshot-2023-06-09-at-11.20.05-2.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=\"Screenshot-2023-06-09-at-11.20.05-2\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/Screenshot-2023-06-09-at-11.20.05-2.png?fit=660%2C371&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/Screenshot-2023-06-09-at-11.20.05-2.png?resize=660%2C371&#038;ssl=1\" alt=\"\" class=\"wp-image-3225\" srcset=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/Screenshot-2023-06-09-at-11.20.05-2.png?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/Screenshot-2023-06-09-at-11.20.05-2.png?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/Screenshot-2023-06-09-at-11.20.05-2.png?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/Screenshot-2023-06-09-at-11.20.05-2.png?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/Screenshot-2023-06-09-at-11.20.05-2.png?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/Screenshot-2023-06-09-at-11.20.05-2.png?w=1320&amp;ssl=1 1320w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Annotate migrations so that when you build your plan, it will do the migration for you<\/li>\n\n\n\n<li>You setup the Model with current Schema and a Migration plan in your app and it will upgrade data when needed.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Utilize schema Macros Evolving schemas<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"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}},"categories":[2,3],"tags":[725,691,680],"class_list":["post-3224","post","type-post","status-publish","format-standard","hentry","category-blog","category-personal-softwareandit","tag-day-5","tag-swiftdata","tag-wwdc23"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2aMa8-Q0","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3224","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=3224"}],"version-history":[{"count":1,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3224\/revisions"}],"predecessor-version":[{"id":3227,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3224\/revisions\/3227"}],"wp:attachment":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/media?parent=3224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/categories?post=3224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/tags?post=3224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}