Apple Introduces SwiftData
Written by Kay Ewbank   
Thursday, 15 June 2023

Apple has introduced the SwiftData framework as a way of making it easier to persist data using declarative code. The framework is integrated with SwiftUI and can be used to query and filter data using regular Swift code.

The framework was announced in a preview version at Apple's WWDC23 conference. Apple says the framework combines Core Data’s proven persistence technology and Swift’s modern concurrency features, and enables developers to add persistence to apps quickly, with minimal code and no external dependencies.

swiftlogo

The data is modeled using regular Swift types with @Model, with no additional files or tools to manage. SwiftData can automatically infer many relationships and you can use clear declarations like @Attribute(.unique) to describe constraints.

swiftdata

By design, SwiftData supplements your existing model classes. The framework provides tools such as macros and property wrappers that can be used to describe your app’s schema in Swift code, removing any reliance on external dependencies such as model and migration mapping files.

Developers can describe the entire model layer (or object graph) for an app, and the framework handles storing the underlying model data, and optionally, syncing that data across multiple devices.

The SwiftData documentation says that the ability to preserve model data across launches is one of the benefits of SwiftData, as most apps define a number of custom types that model the data it creates or consumes, and SwiftData lets you persist that data so it’s available across app launches, and make use of the framework’s integration with SwiftUI to refetch that data and display it onscreen.

To let SwiftData save instances of a model class, a developer would import the framework and annotate that class with the Model macro. The macro updates the class with conformance to the PersistentModel protocol, which SwiftData uses to examine the class and generate an internal schema. Additionally, the macro enables change tracking for the class by adding conformance to the Observable protocol.

CloudKit syncing is supported. Data can be stored in files using DocumentGroup and synced via iCloud Drive, or you can use CloudKit to sync data between devices.

SwiftData is available in preview now.

swiftlogo

More Information

SwiftData Documentation

Related Articles

Apple Releases Swift Playgrounds 4

Swift 5.5 Adds Concurrency Support

Swift 5.2 Improves Performance

Apple Launches Swift Playgrounds

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


PlanetScale Gets Into Vector Search
02/12/2024

PlanetScale, the cloud MySQL-compatible database with advanced scaling capabilities, is now upgraded with vector storage and search.



1000 Programmer's Mugs
06/12/2024

It is legend that programmers run on coffee so what better as a festive gift than a new mug with an appropriate slogan? You could boost your favourite programmer's performance by encouraging increased [ ... ]


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info