{"id":44635,"date":"2024-08-26T21:29:02","date_gmt":"2024-08-27T01:29:02","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=44635"},"modified":"2024-08-26T22:01:00","modified_gmt":"2024-08-27T02:01:00","slug":"using-codable-and-enums-in-swiftdata-models","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2024\/08\/26\/using-codable-and-enums-in-swiftdata-models\/","title":{"rendered":"Using Codable and Enums in SwiftData Models"},"content":{"rendered":"<p><a href=\"https:\/\/fatbobman.com\/en\/posts\/considerations-for-using-codable-and-enums-in-swiftdata-models\/\">Fatbobman<\/a> (<a href=\"https:\/\/www.reddit.com\/r\/swift\/comments\/1erzxb7\/considerations_for_using_codable_and_enums_in\/\">Reddit<\/a>):<\/p>\n<blockquote cite=\"https:\/\/fatbobman.com\/en\/posts\/considerations-for-using-codable-and-enums-in-swiftdata-models\/\">\n<p>In SwiftData&rsquo;s default storage implementation, the method of persisting the <code>people<\/code> attribute is not by converting data into binary format through encoders such as <code>JSONEncoder<\/code> and storing it in a single field (similar to Core Data&rsquo;s <code>Value Transformer<\/code>). Instead, SwiftData creates separate fields for each attribute of Codable data within the table corresponding to the entity (interpreted as converting to Core Data&rsquo;s <code>Composite attributes<\/code>).<\/p>\n<p>[&#8230;]<\/p>\n<p>Such errors indicate that not all complex types conforming to the <code>Codable<\/code> protocol are suitable for SwiftData models. Although some complex <code>Codable<\/code> types may compile correctly, they can lead to inconsistent behavior and anomalies in practice (many developers have reported such issues without a clear pattern).<\/p>\n<\/blockquote>\n<p>It&rsquo;s odd how <code>Codable<\/code> is used as the marker for automatic destructuring, but then SwiftData doesn&rsquo;t actually follow your <code>Codable<\/code> conformance (or even the <code>CodingKeys<\/code> names) to decide how to do it.<\/p>\n\n<blockquote cite=\"https:\/\/fatbobman.com\/en\/posts\/considerations-for-using-codable-and-enums-in-swiftdata-models\/\">\n<p>Due to the non-fully encoding and decoding nature of <code>Codable<\/code> types, altering their properties by adding, removing, or renaming can disrupt SwiftData&rsquo;s lightweight data migration mechanism. This is particularly critical when the application employs SwiftData&rsquo;s built-in cloud synchronization feature, as such modifications may not comply with the cloud synchronization rules, leading to sync failures.<\/p>\n<p>[&#8230;]<\/p>\n<p>Although using enum types directly as model properties is highly convenient, as of iOS 18, SwiftData still does not support using enum types as query predicates.<\/p>\n<\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2024\/07\/26\/swiftdata-and-core-data-at-wwdc24\/\">SwiftData and Core Data at WWDC24<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2023\/06\/12\/swiftdata\/\">SwiftData<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Fatbobman (Reddit): In SwiftData&rsquo;s default storage implementation, the method of persisting the people attribute is not by converting data into binary format through encoders such as JSONEncoder and storing it in a single field (similar to Core Data&rsquo;s Value Transformer). Instead, SwiftData creates separate fields for each attribute of Codable data within the table corresponding [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2024-08-27T01:29:04Z","apple_news_api_id":"6c487283-e689-45ce-a6b8-d7b80b1ff0e9","apple_news_api_modified_at":"2024-08-27T02:01:04Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/AbEhyg-aJRc6muNe4Cx_w6Q","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[71,1774,901,2404],"class_list":["post-44635","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-programming","tag-swift-codable","tag-swift-programming-language","tag-swiftdata"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/44635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=44635"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/44635\/revisions"}],"predecessor-version":[{"id":44641,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/44635\/revisions\/44641"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=44635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=44635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=44635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}