Weekly Comment
From the launch of the first beta version at WWDC 2023, SwiftData has been around for four months in the blink of an eye. In the past month, I have devoted considerable effort to researching and learning it, and have written several articles about it.
SwiftData largely addresses several major criticisms of Core Data. This is mainly reflected in the following aspects: pure code modeling, diversity of property types (automatic mapping of types that conform to RawRepresentable and Encodable), a safer and more elegant experience in concurrent programming, type-safe predicates, and better integration with SwiftUI. Among them, the logic of creating data models and the new approach to concurrent programming have left a deep impression on me.
Unfortunately, ideals are lofty while reality is harsh. Just when I was eager to build a brand new application with SwiftData, the limited predicate expression capabilities became a hindrance. The new predicate API fails to seamlessly work with composite types (resulting in diminished advantages of new property types) and also falls short in representing object relationships. Moreover, predicates belong to the Foundation framework, which adds to the coordination difficulties between the two development teams.
Except for the predicates, poor stability, limited API, and numerous bugs are the problems that have to be dealt with.
As the most important persistence framework in the future Apple ecosystem, SwiftData is designed very elegantly, but its implementation is somewhat hasty, which is undoubtedly regrettable.
I will postpone starting the development of iOS 17+ applications using SwiftData and observe if the predicates and stability are improved in the upcoming updates. However, through my research on the implementation of SwiftData, I have gained a deeper understanding of how to use Core Data in a modern way and how to create Core Data applications that are suitable for migration to SwiftData. It has been a fruitful learning experience.
In the future, I will share several articles related to SwiftData and look forward to its early improvement!
Previous Issue|Newsletter Archive
If you find this weekly report or my blog helpful, I would greatly appreciate your consideration of making a donation through Patreon, or Buy Me a Coffee.
Originals
Concurrent Programming in SwiftData
Concurrent programming in Core Data may not be difficult, but it is full of traps. Even with ample experience in Core Data, a slight negligence can introduce vulnerabilities into the code, making the application unsafe. As the successor to Core Data, SwiftData provides a more elegant and secure mechanism for concurrent programming. This article will introduce ways SwiftData addresses these issues and offers developers a better experience with concurrent programming.
During the writing process, it was found that there was a lot of content to be explained, and it was eventually divided into three parts. The first two parts are Exploring CoreData — From Data Model Creation to Managed Object Instances and Unveiling the Data Modeling Principles of SwiftData.
Recent Selections
Deep Understanding of Observation - Principles, Back Porting, and Performance
At WWDC 23, Apple introduced a brand new Observation framework aimed at addressing the confusion and performance issues of state management in SwiftUI. The way this framework works seems quite magical, even without any special declaration, it allows for property-level subscriptions in Views, thus avoiding unnecessary refreshes. This article will delve into the underlying principles to help you:
Understand the essence and implementation mechanism of the Observation framework
Compare its advantages with previous solutions
Introduce a way to make Observation backward compatible to iOS 14
Explore some considerations and trade-offs in handling SwiftUI state management
This text explains the implementation principle of Observation, but more importantly, the author has created a third-party library called ObservationBP. By wrapping the source code of Observation, developers can use the functionality of Observation in lower system versions.
Great SwiftUI
This is an article on SwiftUI.wtf that the author recently updated. Aviel Gross describes this article as a collection of best practices, tips, and tricks for the SwiftUI codebase to fully leverage its advantages. The article is divided into three sections:
Behavior: Make your code do what you want it to do, or what you think it should be doing
Structure: How to layout your code (where to put logic, how to build the body etc)
Performance: Make your app run fast, and increase its stability
The experiences and tips listed in the article have a considerable universality, but they are not absolute rules. In individual scenarios or needs, adjustments should be made based on the actual situation. Therefore, understanding the principles behind them is more important than memorizing the techniques.
"Cracking" the Easter Eggs in visionOS Beta 1.4
In this article, the author introduces some "Easter eggs" about the VisionOS system that he discovered from the simulator files of version 1.4. These include system applications, spatial photos and panorama photos in the album, from Optic ID to EyeSight to Digital Human, wearing prompts (RealityCoverSheet), and lens prescriptions (CorePrescription), among other content. Through the analysis of this information, we can get a glimpse of some design details and progress of visionOS.
Designing for spatial computing: from iOS and iPadOS to visionOS
Apple Vision Pro represents a significant revolution in the field of spatial computing, providing users with unprecedented interactivity and immersion. In this article, Emanuele Agosta will explore the design principles of spatial computing and the key considerations for seamlessly transitioning iOS/iPadOS applications to visionOS applications, while providing foundational guidance.
Working With XcodeKit <> SwiftLeeds
As the author of EditKit Pro, Aryaman Sharda was invited to give a talk on Working with XcodeKit at SwiftLeeds. In his presentation, he shared some experiences and tips on developing Xcode Source Editor Extensions. This article provides the presentation slides used during the talk.
EditKit Pro is an Xcode Source Editor Extension that provides a set of tools to help developers write better, cleaner, and more efficient code. It integrates many features such as quick code formatting, creating Codable models, generating mock data, sorting lines by length, organizing SwiftUI views, and more. You can get the source code of EditKit Pro here or directly download and install it from the App Store.
Interoperability: Swift’s Super Power
In this article, Saleem Abdulrasool shares their experience of developing Windows applications using Swift. They demonstrate the potential of Swift as a cross-platform language by showcasing how to access Windows API and COM interfaces through the built-in interoperability features of the Swift language.
Swift 5.9 introduces bidirectional interoperability support between Swift and C++. By generating C++ header files, Swift APIs can be exposed to C++ in a similar C++ interface format, greatly simplifying the process of incorporating Swift into existing C++ code. This feature is enabled by default and does not require any additional code modifications. This new functionality allows Swift to seamlessly integrate into the existing C++ ecosystem while still maintaining its own distinctive features.
肘子的话
从在 WWDC 2023 上推出第一个测试版开始,一转眼,SwiftData 诞生已经四个月了。最近的一个多月,我花了相当大的精力对其进行了研究和学习,并写了几篇关于它的文章。
SwiftData 在相当程度上解决了 Core Data 中几个主要被诟病的问题。主要体现在以下几个方面:纯代码建模、属性类型的多样性(自动映射符合 RawRepresentable 和 Encodable 的类型)、更安全优雅的并发编程体验、类型安全的谓词以及与 SwiftUI 更好的配合等。其中,数据模型的创建逻辑和新的并发编程方式给我留下了深刻的印象。
遗憾的是,理想很丰满,现实很骨感。就在我满怀热情地想要用 SwiftData 构建全新的应用程序时,有限的谓词表述能力却成为了阻碍。新的谓词 API 无法完美地与合成类型配合(导致属性的新类型优势大打折扣),在对象关系的表述方面也存在不足。而且,谓词属于 Foundation 框架,也增加了两个开发团队之间的协调难度。
除了谓词外,较差的稳定性、有限的 API 、为数不少的 Bug 都是不得不面对的问题。
作为未来苹果生态系统中最重要的持久化框架,SwiftData 的设计非常精巧,但实现却很仓促。这无疑让人感到遗憾。
我会暂缓启动开发使用 SwiftData 的 iOS 17+ 应用,观察谓词和稳定性能否在接下来的升级中得到改善。不过通过对 SwiftData 实现的研究,我对如何以现代的方式使用 Core Data,以及如何创建适合迁移到 SwiftData 的 Core Data 应用有了更深入的了解,也算收获颇丰。
之后我还会分享几篇与 SwiftData 有关的内容,期待 SwiftData 的完整体能够尽早的到来!
如果您发现这份周报或我的博客对您有所帮助,可以考虑通过 Patreon, Buy Me a Coffee, 微信 支持我的创作。
原创
SwiftData 中的并发编程
在 Core Data 中进行并发编程可能并不困难,但是充满了陷阱。即使对 Core Data 有充分的经验,稍有疏忽也可能在代码中埋下隐患,从而使应用程序变得不安全。SwiftData 作为 Core Data 的继任者,提供了一种更加优雅、更加安全的并发编程机制。本文将介绍 SwiftData 是如何解决这些问题的,并为开发者提供更好的并发编程体验。
在写作过程中,发现要阐述的内容比较多,最终分成了三篇。前两篇分别为 CoreData 探秘 - 从数据模型构建到托管对象实例 和 揭秘 SwiftData 的数据建模原理 。
近期推荐
深入理解 Observation - 原理,back porting 和性能
在 WWDC 23 中,Apple 推出了全新的 Observation 框架,旨在解决 SwiftUI 上的状态管理混乱和性能问题。这个框架的工作方式看似非常神奇,甚至无需特别声明,就能在 View 中实现属性粒度的订阅,从而避免不必要的刷新。本篇文章将深入探讨背后的原理,帮助您:
理解 Observation 框架的实质和实现机制
比较其与之前解决方案的优势所在
介绍一种把 Observation 前向兼容到 iOS 14 的方式
探讨在处理 SwiftUI 状态管理时的一些权衡与考虑
本文除了讲解 Observation 的实现原理外,更重要的是,作者实现了一个第三方库 ObservationBP ,通过对 Observation 源代码的包装,让开发者可以在更低的系统版本中使用到 Observation 的功能。
Great SwiftUI
这是 SwiftUI.wtf 上的一篇文章,作者在不久对其进行了更新。Aviel Gross 对这篇文章的描述是:这是一份关于 SwiftUI 代码库的最佳实践、技巧和窍门的集合,以充分发挥其优势。文章分为三个部分:
行为:使你的代码实现你想要的功能,或者你认为应该实现的功能
结构:如何布局你的代码(放置逻辑的位置,如何构建主体等)
性能:使你的应用程序运行快速,并增加其稳定性
文章中列出的经验、技巧具备相当的普适性,但并非铁律。在个别场景或需求中,要根据实际情况做出调整。因此掌握其中的原理比熟记技巧更加重要。
「破解」visionOS Beta 1.4 中的彩蛋
在这篇文章中,作者介绍了他从 visionOS Beta 1.4 版本的模拟器文件中挖掘出的一些关于 VisionOS 系统的「彩蛋」。包括系统应用、相册中的空间照片&全景照片、从 Optic ID 到 EyeSight 到数字人、佩戴提示(RealityCoverSheet)、配镜处方(CorePrescription)等内容。通过对其中信息的分析,可以一窥 visionOS 的一些设计细节和进展。
Designing for spatial computing: from iOS and iPadOS to visionOS
Apple Vision Pro 标志着空间计算领域的重大变革,其为用户提供了前所未有的互动性和沉浸感。在本文中,Emanuele Agosta 将探讨空间计算的设计原则以及实现 iOS/iPadOS 应用程序无缝过渡到 visionOS 应用程序的关键考虑因素并提供基础指导。
Working With XcodeKit <> SwiftLeeds
作为 EditKit Pro 的作者,Aryaman Sharda 受邀在本次 SwiftLeeds 上以 Working with XcodeKit 为主题进行了演讲。在演讲中他分享了开发 Xcode Source Editor Extensions 的一些经验和技巧,并在本文中提供了此次演讲使用的演示文稿。
EditKit Pro 是一个 Xcode Source Editor Extension,它提供了一组工具,可以帮助开发者编写更好、更干净、更高效的代码。集成了诸如快速格式化代码、创建 Codable 模型、生成模拟数据、按长度对行排序、整理 SwiftUI 视图等众多功能。可以在此处获取 EditKit Pro 的源代码,或直接通过 App Store 下载安装。
Interoperability: Swift’s Super Power
在这篇文章中,Saleem Abdulrasool 分享了他们使用 Swift 开发 Windows 应用的经验,通过介绍如何通过 Swift 语言内置的互操作性功能来访问 Windows API 和 COM 接口,展示了 Swift 作为一门跨平台语言的潜力。
Swift 5.9 引入了 Swift 与 C++ 的双向互操作性支持。通过生成 C++ 头文件,Swift API 可以以近似的 C++ 接口的形式暴露给 C++ 使用,极大简化了在现有 C++ 代码中引入 Swift 的过程。该特性默认开启,无需额外修改代码。新功能让 Swift 在保持自身特色的同时,快速融入现有的 C++ 生态。