WWDC 23, First Impressions of SwiftUI 5 and SwiftData

fatbobman ( 东坡肘子)
ITNEXT
Published in
6 min readJun 8, 2023

--

WWDC 2023 is in full swing. Apple has not only introduced new hardware products in new forms, but also launched several stunning new frameworks. This article will talk about my initial impressions of SwiftUI 5.0 and SwiftData in this year’s WWDC.

A Chinese version of this post is available here.

In light of the fact that my blog, Fatbobman’s Blog, now offers all articles in English, starting from April 1, 2024, I will no longer continue updating articles on Medium. You are cordially invited to visit my blog for more content.

SwiftUI

If the upgrades from SwiftUI 1.0 to 4.0 were minor fixes, then Apple’s efforts on SwiftUI 5.0 this year can be considered a major redesign.

This year’s improvements to SwiftUI are significant, with some changes being revolutionary.

New Data Flow Declaration and Injection Methods

Using Swift 5.9’s new features, for reference types of source of truth, simply use @Observable to mark them, and views will respond to changes in the data source at the property level. This fundamentally solves the efficiency issues affecting SwiftUI applications (such as excessive computation), allowing developers to design data structures more freely and inject data sources as they wish.

Unfortunately, this new feature can only be implemented in SwiftUI 5. If you plan to develop applications for iOS 17+, you should immediately abandon declarations such as @ObservableObject.

As there are now two different logic declarations for data sources in the same system, this has caused more confusion for beginners.

Revolutionary animation and visual effects upgrade

In this update, SwiftUI has added some advanced animation and visual features that were previously lacking, and Apple has significantly updated its internal implementation of animation, transitions, Shapes, effects, and more.

This update brings animation completion callbacks, phased animations, keyframe animations, a brand new Transition protocol (supporting transition states), a brand new Shape protocol (supporting Shape operations), a brand new TransactionKey (supporting custom transaction properties), Shader support (making it extremely easy to implement certain special effects), type-safe image and color resource types (Assets will automatically generate corresponding code), convenient Symbol animations, a brand new CustomAnimation protocol (supporting custom animation functions), spring animations, and many other new features. In short, what limits animation or visual effects will no longer be SwiftUI’s ability, but the developer’s creativity.

Greatly improved the control power of ScrollView

In this upgrade, a new dynamic scroll positioning system for ScrollView was introduced (not relying on ScrollViewReader and explicit ID declaration), a one-time positioning system (directly positioning to a specific location of the scroll view after the view enters, can only be used once), a brand-new scroll bar control (blinking), customizable display status of row views at the top of the scroll area and the display area (for example, to achieve a visual effect similar to the scroll-to-top subview shrink effect in watchOS), support for paging scrolling (long-awaited by developers), custom indentation of scroll content, and many other features.

Don’t miss out on the latest updates and excellent articles about Swift, SwiftUI, Core Data, and SwiftData. Subscribe to fatbobman’s Swift Weekly and receive weekly insights and valuable content directly to your inbox.

Other features

There are so many upgrade contents this time that many new features are not listed in the update document provided by Apple. In the coming period, there should be many articles on the Internet to further explain and interpret these features.

However, it is regrettable that Apple did not fully utilize the @_backDeploy function of Swift. In SwiftUI 5.0, only a few unimportant functions or types have implemented compatibility with lower versions: topBarLeading: SwiftUI.ToolbarItemPlacement, topBarTrailing: SwiftUI.ToolbarItemPlacement, accessoryBar<ID>, horizontalSizeClass, verticalSizeClass, typeSelectEquivalent.

Without considering compatibility with old versions, I think the upgrade of SwiftUI 5.0 can score 95 points (full score of 100 points). However, considering that many developers will not be able to use these new features for a considerable period of time, their mood will be exceptionally low.

SwiftData

After a long wait from developers, Apple has finally released SwiftData, an object graph management and persistence framework based on Swift. As predicted, Apple did not create a completely new logic for data storage, but rather SwiftData is essentially an official Swift wrapper library for Core Data, implemented using new features in Swift 5.9.

From my usage in the past couple of days, it seems that with further improvements and enhancements to its functionality and stability, it will definitely bring more convenience to developers.

Here are some questions and considerations I have compiled regarding SwiftData (originally posted in a tweet, without a more systematic categorization):

  • Cloud sync for public and shared data is not yet supported.
  • In the current version, data created through other contexts (ModelContext) is not automatically merged into the view context.
  • There are problems with custom migration plans in the first version.
  • SwiftData can be mixed with Core Data code, but entityVersionHashesByName must be used to determine if the models are identical.
  • Neither PersistentModel nor ModelContext are Sendable (ModelContainer is Sendable), and they are thread-limited like Core Data.
  • Even if a transaction is attempted to maintain thread consistency in a new context, an error will be forced when com.apple.CoreData.ConcurrencyDebug 1 is enabled (even in a newly created actor).
  • Attribute(.unique) used in the demo is not suitable for synchronization scenarios due to CloudKit limitations.
  • The current functionality is less than that of Core Data, with no new additions.
  • The nature of PersistentModel is similar to that of Observed states created through macros, and it can directly drive view updates (property wrappers are not necessary when passing).
  • Derived options for Attribute have been deprecated.
  • The Model can be converted to SwiftData code using the Model Editor in Xcode, but there are currently many problems when there are multiple options or when the property type is transformable.
  • The Index set in the Model cannot currently be converted (corresponding code can be generated, but it is not yet fully attributed).
  • All startup parameters for Core Data are currently applicable.
  • There are issues with the automatic saving of modelContext, and the save method should still be called.
  • When mixed with Core Data Stack, persistent history tracking must be enabled on the Core Data side.
  • Query (an alternative to FetchRequest) does not provide a method for dynamically switching predicates and sorting.

From the perspective of code style and implementation, SwiftData has a bright future, but due to the existence of several issues at present, it is not recommended to use SwiftData directly, even if you plan to develop iOS 17+ applications.

To work with SwiftData, Core Data has made a minor upgrade, and one of the features that is worth mentioning is custom composite types.

However, there have been no adjustments in the client-side of Core Data with CloudKit API, which is disappointing.

Happy or Helpless

At this year’s WWDC, Apple brought about significant changes to SwiftUI and introduced SwiftData, which developers have longed for. When I first saw this information, I was excited, but soon I calmed down and felt helpless.

For most developers, once they can use these new features in their applications, Apple may bring more new temptations. The new features of SwiftUI greatly expand its expressiveness, but also increase its learning curve, especially for beginners. Although SwiftData simplifies the development of Core Data, as a new framework, its stability and robustness still need to be further verified.

In the past few years, Apple has launched a series of new technologies, and developers need to continuously learn and adapt, which invisibly increases the development cost and risk. Although Apple’s new technologies generally have practical value, developers also need to carefully weigh between chasing new and maintaining stability.

However, for me, this year’s WWDC provided a lot of material for learning and writing. In the next period of time, I will introduce and explore SwiftUI, SwiftData, and several new frameworks that I am interested in — TipKit and CKSyncEngine — on my blog.

If you found this article helpful or enjoyed reading it, consider making a donation to support my writing. Your contribution will help me continue creating valuable content for you.
Donate via Patreon, Buy Me aCoffee or PayPal.

Want to Connect?

@fatbobman on Twitter.

--

--

Blogger | Sharing articles at https://fatbobman.com | Publisher of a weekly newsletter on Swift at http://https://weekly.fatbobman.com