At the 2023 Worldwide Developers Conference (WWDC), Apple launched the highly anticipated new generation data management framework — SwiftData. As the successor to Core Data, can SwiftData play a key role in the Apple ecosystem? With WWDC 2024 approaching, this article will evaluate the overall performance of SwiftData since its initial release during the Xcode 15 period (i.e., its first major version), and provide a forecast of its future development.
Toward the Future: SwiftData, A Modern Data Management Framework
SwiftData was not built entirely from scratch; its core technology is still based on Core Data. In the first WWDC 2023 session about SwiftData, Apple did not elaborate on this aspect.
Initially, I was confused and even suspicious about why Apple did not clarify the connection with Core Data right from the start. However, as I delved deeper into SwiftData, I gradually understood Apple’s strategy. While SwiftData is built on the proven technological foundation of Core Data, its overall design and presentation signify the birth of a completely new data management framework. Apple’s choice to downplay the connection with Core Data aims to free developers from the preconceived notions associated with Core Data, enabling a better understanding of SwiftData’s team’s fresh interpretation of a modern data management framework. This strategy not only emphasizes SwiftData’s status as an independent framework but also clearly defines its future role within the Apple ecosystem.
For developers with extensive Core Data experience, they might find it challenging to grasp and apply the new programming philosophy introduced by SwiftData, similar to the challenges faced by UIKit developers transitioning to SwiftUI.
SwiftData not only simplifies some of the complex and seldom-used features of Core Data but also introduces many practices that align with modern programming paradigms. The framework fully leverages new features of the Swift language, demonstrating how to build secure, stable, and elegant code, and laying the foundation for its central role in the Apple ecosystem.
Therefore, SwiftData can be regarded as a new and modernized data management framework, with the potential to play a pivotal role in the Apple ecosystem for over a decade.
Hastily Launched: The Real Issues Facing SwiftData
Although SwiftData’s design is highly visionary, its launch was clearly rushed, and its first version not only lacks some important features, but several key issues have also severely impacted its performance and usability:
-
Immature Predicate Conversion Capability:
SwiftData introduced a new Predicate system provided by Foundation, which is safer and more ergonomically sound. Although it is not as comprehensive as the traditional NSPredicate yet, it meets the retrieval declaration needs of most developers. However, as more applications activate data cloud synchronization, increasingly, model properties are declared as optional to meet synchronization requirements. Yet, SwiftData’s current performance in converting predicates that include optional values (transforming predicates into SQL commands) is poor, especially when handling “to-many” relationships with optional predicates. This deficiency not only severely impacts SwiftData’s usability but also significantly restricts the functionalities that applications using SwiftData can offer.
-
UI Response to Data Updates is Problematic:
SwiftData utilizes Swift’s modern concurrency model to provide an elegant mechanism for handling concurrent data operations, allowing developers to encapsulate concurrent data operations through
@ModelActor
, thus effectively preventing the common concurrency crashes seen in Core Data. However, despite updates over the past year, data update operations performed through@ModelActor
often fail to prompt timely responses in SwiftUI views. To circumvent this issue, developers are forced to abandon@ModelActor
or resort to various tricks to force view updates, not only wasting this feature’s potential but also severely impacting the application’s efficiency. -
Model Validation and Conversion Issues:
In Core Data, when data network synchronization is enabled, Xcode’s model editor automatically verifies whether models comply with synchronization standards. By contrast, SwiftData uses a purely code-based modeling approach, requiring developers to manually code for model validation. This not only increases development complexity but might also lead to developers, who are inadequately informed about synchronization standards, discovering mismatches in their models near project completion, necessitating extensive modifications. Additionally, occasional conversion errors may occur when translating code-declared models into the underlying Core Data-recognizable
NSManagedObjectModel
, especially in the absence of explicit inverse relationship annotations (which could otherwise be omitted). These errors are often hard to detect, and troubleshooting them requires a significant investment of time and effort. -
Codable Support and Unclear Data Storage:
SwiftData allows developers to use types that conform to the Codable protocol directly as model properties, significantly clarifying models and simplifying operations. However, the lack of a clear guideline and defined storage correspondence from the official side presents numerous challenges in practical use. Not all types conforming to the Codable protocol can be reliably converted, occasionally leading to application crashes. Furthermore, the absence of clear official guidance might cause issues for developers adjusting Codable types, especially in data cloud synchronization scenarios, where new model versions may not be eligible for seamless migration (failing to utilize lightweight migration).
Despite the fact that SwiftData’s first version is not as comprehensive as Core Data, if the framework operates stably and meets project requirements, these shortcomings do not pose significant issues. However, as mentioned earlier, existing problems may lead to application anomalies and unexpected challenges during development.
Overall, given the rushed launch of SwiftData, its first version has not fully met expectations and has not adequately demonstrated the capabilities and stability expected of a data management framework. Therefore, it is crucial to conduct necessary updates and enhancements to SwiftData at the upcoming WWDC 2024.
Applicability: Who is the First Version of SwiftData Suitable For?
As a developer who has invested considerable effort in researching SwiftData, I fully recognize its ingenious design and bright prospects. However, I must admit that the first version of SwiftData is not suitable for all developers and projects.
-
For Beginners (with no experience in data management frameworks): Not Recommended
Developers unfamiliar with data management frameworks might find that due to a lack of understanding of SwiftData’s capabilities and limitations, they easily encounter insurmountable obstacles during project development.
-
For Projects with Complex Requirements: Not Recommended
If a project has high demands for a data management framework, the maturity and comprehensive functionality of Core Data make it a better choice than the first version of SwiftData. Although it is possible to integrate SwiftData and Core Data within a project, the cost and effort involved may not be justified.
-
For Projects with Undefined Requirements: Not Recommended
As project functionalities continue to expand, it is likely that developers will find the first version of SwiftData unable to meet the evolving development needs.
-
For Projects with Clear Requirements and Unaffected by Current Issues in SwiftData: Recommended
Despite its limitations in the first version, SwiftData is suitable for projects with simple functional requirements that do not involve complex relationships or performance issues, provided you can avoid the major issues present in the first version of SwiftData. Additionally, if the project does not utilize data network synchronization features, some of SwiftData’s shortcomings can be further mitigated.
-
For a Specific Audience: Worth Trying
For those developers who fully understand the project requirements and are well aware of the limitations and shortcomings of the first version of SwiftData, particularly those with extensive Core Data development experience, using the first version of SwiftData for commercial project development, despite the challenges, can be rewarding as it provides a sense of achievement in overcoming difficulties.
Although the performance of the first version of SwiftData did not fully meet expectations, its advanced design concepts and elegant implementation are still worth learning from. Even if you currently do not need a data management framework, or are not considering using SwiftData, studying and understanding it can still have a positive impact on your future development work.
Over the past year, I have written more than a dozen articles on SwiftData. These articles thoroughly explore the principles and design strategies of SwiftData, and should be very helpful for developers who wish to gain a deeper understanding of this framework.
Looking Ahead: The Upcoming Changes in SwiftData
As WWDC 2024 approaches, I am hopeful that the forthcoming updates will address some of the major issues revealed in the first version of SwiftData.
As previously mentioned, Apple is in the process of creating SwiftData as a brand new data management framework, specifically designed for the current and future Apple ecosystem, particularly for environments that include mobile applications and high-performance hardware like high-speed CPUs and solid-state drives. Therefore, many of the distinctive features found in Core Data might not be included in SwiftData due to changes in the market and hardware environment. Additionally, SwiftData may introduce more refined encapsulations to simplify some of the complex operations of the past.
Thus, I am curious about how Apple will add new features to SwiftData, not just which features will be added.
Based on the development experience with SwiftUI, it may take another two to three years for SwiftData to become a key official data management framework within the Apple ecosystem. This process will be lengthy and challenging, but it will ultimately provide developers with powerful tools to build future applications.
Advice for Current SwiftData Developers
If you have already implemented SwiftData in your project and it is performing well, congratulations 🎉 ! Furthermore, I recommend that you delve deeper into the major issues currently faced by the first version of SwiftData and do your best to mitigate these in upcoming updates. Finally, if possible, consider raising the minimum system requirements of your project after the release of iOS 18 (or subsequent systems on other platforms) to take advantage of potential optimizations and improvements.
Appendix: Some Key Features Missing, Major Issues, and Partial Temporary Solutions in the First Version of SwiftData
- Lazy Loading (Faulting): SwiftData does not support the faulting feature typical in Core Data for lazy loading of data.
- Batch Data Operations: Currently, there is no support for batch data operations, but this can be achieved by integrating with Core Data.
- Multiple Persistence Storage Support: Prior to iOS 17.4, SwiftData did not support multiple persistence storages.
- Advanced Query Features: Does not support advanced query features such as
group
,having
,distinct
. If needed, these can be addressed through SwiftDataKit. - Predicate Functionality Limitations: After enabling synchronization features, the capability to express predicates in many-to-many relationships is restricted.
- Public Database and Data Sharing: Does not support synchronizing public databases and sharing data.
- Performance Issues: Operations on to-many relationships can trigger severe performance issues.
- View Update Response: Views are unable to respond to data updated within
@ModelActor
. Refer to Solution One and Solution Two. - Predicate Merging: Does not support predicate merging. Refer to this solution.
- Codable Support: When using types that comply with the Codable protocol, ensure that all properties of the type are consistent with the fundamental properties of SQLite (such as integers, floats, dates, etc.).
- Codable as Query Criteria: Properties of Codable types cannot be used as query criteria (though Core Data supports Composite Attributes).
- Model Validation: When developing data models that support synchronization, validate the model first.
- Enum Storage: It is not recommended to use enums directly as storage types because they cannot be used as query criteria; it is best to save their corresponding rawValue.
- Insufficient Event Handling: SwiftData does not support adding custom logic during lifecycle events such as
didSave
orwillSave
. This limitation affects the ability to perform specific actions during data saving processes. A workaround is available in the solution guide. - Cascade Deletion Issue: SwiftData does not automatically delete related association data when performing delete operations with the
cascade
deletion rule. This functionality can be achieved using SwiftDataKit. - System Version Requirements: It is advisable to raise the minimum system requirements of your project, recommended to be at least iOS 17.2.