Beginners Guide to SwiftData
by
Adam Henry
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
Mobile Dev Japan July 26, 2024 Adam Henry Safie Inc. Development Division 2 > Mobile Group > iOS Team Beginners Guide to SwiftData
Slide 2
Slide 2 text
© Safie Inc.| 2 Adam Henry (@monolithic_adam) ● iOS Engineer ● Joined Safie 2024/3 ○ Previously STORES, Chatwork, Buyma, etc. ● Living in Japan since 2011 ● Hobbies ○ Playing with my kids 🧒👧 ○ Games 🎮 ○ Tennis 🎾 ○ Sake 🍶🍺
Slide 3
Slide 3 text
© Safie Inc.| SwiftData 3
Slide 4
Slide 4 text
© Safie Inc.| SwiftData (iOS 17.0+) 4 SwiftData is a framework that allows you to write your model code declaratively to add managed persistence and efficient model fetching. So, it’s CoreData but way easier.
Slide 5
Slide 5 text
© Safie Inc.| @Model @Attribute @Query ModelContainer & ModelContext #Predicate The Basics 5
Slide 6
Slide 6 text
© Safie Inc.| SwiftData Macros (@Model, @Attribute, etc.) 6
Slide 7
Slide 7 text
© Safie Inc.| 7 @Model A macro that defines your model’s schema from Swift code. SwiftData natively adapts your value type properties to be used as attributes. Basic and more complex values are able to be used as well (structs, enums, codable types like collections)
Slide 8
Slide 8 text
© Safie Inc.| 8
Slide 9
Slide 9 text
© Safie Inc.| 9 @Attribute Using this you can add uniqueness constraints to your schema
Slide 10
Slide 10 text
© Safie Inc.| @Relationship, @Transient are other macros you can use to change your schema. @Relationship - you can control the choice of inverses and specify delete propagation rules. @Transient - allows you to choose a property not to be stored by SwiftData Other Macros 10
Slide 11
Slide 11 text
© Safie Inc.| @Query 11
Slide 12
Slide 12 text
© Safie Inc.| Using the @Query property wrapper you can easily load and filter anything stored in your database with a single line of code. SwiftData supports observable for your modeled properties. @Query 12
Slide 13
Slide 13 text
© Safie Inc.| 13
Slide 14
Slide 14 text
© Safie Inc.| It didn’t play nice with enums when I built the app with SwiftData 1.0... If anyone knows a way to get it to be able to filter by an enum case please talk to me after my LT! @Query A problem I found 14
Slide 15
Slide 15 text
© Safie Inc.| ModelContainer & ModelContext 15
Slide 16
Slide 16 text
© Safie Inc.| The ModelContainer is where you describe how data is stored, or persisted, on a device. ModelContext tracks and manages a models state in memory ModelContainer & ModelContext 16
Slide 17
Slide 17 text
© Safie Inc.| 17
Slide 18
Slide 18 text
© Safie Inc.| 18
Slide 19
Slide 19 text
© Safie Inc.| 19
Slide 20
Slide 20 text
© Safie Inc.| SwiftData 2.0 (WWDC 2024) 20
Slide 21
Slide 21 text
© Safie Inc.| Tracking Model Changes with SwiftData History Creating your own custom Data Store (for example: JSONDataStore) #Unique Macro Preview Modifier Compound Predicates Add Index for commonly used KeyPaths or combination of KeyPaths (More Efficient/Faster Querying!) SwiftData 2.0 (WWDC 2024) New Features! 21
Slide 22
Slide 22 text
© Safie Inc.| Preview Modifier 22
Slide 23
Slide 23 text
© Safie Inc.| 23
Slide 24
Slide 24 text
© Safie Inc.| 24
Slide 25
Slide 25 text
© Safie Inc.| All iOS 18.0+ See you in a few years... 25
Slide 26
Slide 26 text
© Safie Inc.| What’s New in SwiftData - https://developer.apple.com/wwdc24/10137 Create a custom data store with SwiftData - https://developer.apple.com/wwdc24/10138 Track model changes with SwiftData history - https://developer.apple.com/wwdc24/10075 SwiftData 2.0 (WWDC 2024) References 26
Slide 27
Slide 27 text
© Safie Inc.| 27 I made an example App! It is a simple budget app that uses SwiftData to persist its data.
Slide 28
Slide 28 text
© Safie Inc.| 28 For Funsies I did a poll and 100% were GUI users
Slide 29
Slide 29 text
© Safie Inc| We are hiring! More info : https://open.talentio.com/r/1/c/safie/pages/84134 29 We are looking for Android / iOS engineers!
Slide 30
Slide 30 text
セーフィーは日本中、世界中のカメラの映像をクラウド化し 自分のため、社会のために誰もが活用できる映像プラットフォームを提供します。