{"id":3172,"date":"2023-06-08T13:38:42","date_gmt":"2023-06-08T17:38:42","guid":{"rendered":"https:\/\/michaelrowe01.com\/?p=3172"},"modified":"2023-06-08T14:59:35","modified_gmt":"2023-06-08T18:59:35","slug":"build-an-app-with-swiftdata","status":"publish","type":"post","link":"https:\/\/michaelrowe01.com\/index.php\/blog\/build-an-app-with-swiftdata\/","title":{"rendered":"Build an app 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=\"3173\" data-permalink=\"https:\/\/michaelrowe01.com\/index.php\/blog\/build-an-app-with-swiftdata\/attachment\/%ef%a3%bfwwdc23-20\/\" data-orig-file=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-20.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-20\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-20.png?fit=660%2C371&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-20.png?resize=660%2C371&#038;ssl=1\" alt=\"\" class=\"wp-image-3173\" srcset=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-20.png?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-20.png?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-20.png?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-20.png?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-20.png?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-20.png?w=1320&amp;ssl=1 1320w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/figure>\n\n\n\n<p>Check out \u201cMeet SwiftData\u201d to cover the basics.&nbsp; This is a code along session (<a href=\"https:\/\/developer.apple.com\/wwdc23\/10154\">https:\/\/developer.apple.com\/wwdc23\/10154<\/a>) and you should download the code from <a href=\"https:\/\/developer.apple.com\/documentation\/SwiftUI\/Building-a-document-based-app-using-SwiftData\">https:\/\/developer.apple.com\/documentation\/SwiftUI\/Building-a-document-based-app-using-SwiftData<\/a>&nbsp;<\/p>\n\n\n\n<p>The code along get\u2019s you to build a cross platform code flashcard app. &nbsp;<\/p>\n\n\n\n<p>Meet the App<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This is the flash card app located at the above links.<\/li>\n<\/ul>\n\n\n\n<p>SwiftData models<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If you are converting to SwiftData you will change your class to @Model and remove ObservableObject and @Published from it.&nbsp; Then in the Views were you update the object change from @ObservedObject to @Bindable<\/li>\n\n\n\n<li>For more on this. Check out \u201cDiscover Observation with SwiftUI\u201d session\u00a0 &#8211; <a href=\"https:\/\/michaelrowe01.com\/index.php\/blog\/discover-observation-in-swiftui\/\">Discover Observation in SwiftUI<\/a>\u00a0<\/li>\n<\/ul>\n\n\n\n<p>Querying models to display in UI<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Changing your content view from @State to @Query and removing the sample data assignment allows you to now directly get your data from SwiftData<\/li>\n\n\n\n<li>@Query is a property wrapper that gets data from Swift Data and triggers view updates,&nbsp; You can add simple syntax for (sort: \\.ValueoSortOn) (order: ) and (filter:), and you can have multiple @Query properties in a view.<\/li>\n\n\n\n<li>.modelContainer(for: NameOfClass.self) this will setup a model container, which creates the whole storage stack.&nbsp; Every view can have a single model container.&nbsp; If you don\u2019t have set one up, it won\u2019t save or query.&nbsp; Just set it up in the WindowGroup scene.<\/li>\n\n\n\n<li>You should update your Previews with sample data.&nbsp; Creating an in memory set of sample data achieves this.&nbsp; Look at how this is done in the \u201cPreviewSampleData.swift\u201d file in the project (This failed for me and persisted on both the starter project code along, and the completed final project provided above, I am guessing this may be a Xcode bug).<\/li>\n<\/ul>\n\n\n\n<p>Creating and updating<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You need to accept the modelContext of the view in order to do saves and updates.&nbsp; Just grab it from the Environment<\/li>\n\n\n\n<li>Save is automatic for you<\/li>\n<\/ul>\n\n\n\n<p>[Bonus] Document-based apps<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Changing to document based app, you can turn the flash card deck as separate instances. To do this, you will have to define the package type but it is almost trivial doing this.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Check out \u201cMeet SwiftData\u201d to cover the basics.&nbsp; This is a code along session (https:\/\/developer.apple.com\/wwdc23\/10154) and you should download the code from https:\/\/developer.apple.com\/documentation\/SwiftUI\/Building-a-document-based-app-using-SwiftData&nbsp; The code along get\u2019s you to build a cross platform code flashcard app. &nbsp; Meet the App SwiftData models Querying models to display in UI Creating and updating [Bonus] Document-based apps<\/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":[716,109,691,680],"class_list":["post-3172","post","type-post","status-publish","format-standard","hentry","category-blog","category-personal-softwareandit","tag-code-along","tag-day-4","tag-swiftdata","tag-wwdc23"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2aMa8-Pa","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3172","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=3172"}],"version-history":[{"count":1,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3172\/revisions"}],"predecessor-version":[{"id":3174,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3172\/revisions\/3174"}],"wp:attachment":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/media?parent=3172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/categories?post=3172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/tags?post=3172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}