Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Beginners Guide to SwiftData

Beginners Guide to SwiftData

A lightning talk that gives a brief overview of the basics of SwiftData with examples/example app.
For Mobile Dev Japan #3 on 2024/07/26. https://mobiledev-japan.connpass.com/event/323178/

Adam Henry

July 26, 2024
Tweet

Other Decks in Technology

Transcript

  1. Mobile Dev Japan July 26, 2024 Adam Henry Safie Inc.

    Development Division 2 > Mobile Group > iOS Team Beginners Guide to SwiftData
  2. © 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 🍶🍺
  3. © 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.
  4. © 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)
  5. © Safie Inc.| 9 @Attribute Using this you can add

    uniqueness constraints to your schema
  6. © 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
  7. © 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
  8. © 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
  9. © 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
  10. © 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
  11. © 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
  12. © Safie Inc.| 27 I made an example App! It

    is a simple budget app that uses SwiftData to persist its data.