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


Remembering Thomas Kurtz, Co-creator of BASIC
15/11/2024

Thomas Eugene Kurtz, the co-founder of the BASIC programming language, has died at the age of 96. BASIC, which was developed for the purpose of education, popularized computer programming making it ac [ ... ]



IBM Opensources AI Agents For GitHub Issues
14/11/2024

IBM is launching a new set of AI software engineering agents designed to autonomously resolve GitHub issues. The agents are being made available in an open-source licensing model.


More News

espbook

 

Comments




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