SwiftData, Apple’s long-awaited new framework for data persistence, was finally announced last month at WWDC 2023. I say long-awaited because I’ve been waiting for a Swift-first version of Core Data for almost an entire decade, ever since Swift came out in 2014.
From the very beginning, a deep rift emerged between Swift and Core Data. Core Data, by means of
@dynamic in Objective-C and later
@NSManaged in Swift, 100% relies on the dynamic nature of the Objective-C runtime for Core Data to work at all. This led to some serious awkwardness when Swift came along, like needing to mark almost everything in your Core Data model optional.
This rift was so wide that I was sure we’d see a Core Data replacement soon after Swift came out. I didn’t expect it to take 9 years, but after seeing the Swift features that SwiftData relies on, the delay makes sense. If I were to guess, I bet Apple wanted to first roll out Swift macros to create the models with annotations (
Relationship) and actors to manage concurrency. And, of course, they needed a replacement for
NSSortDescriptor, the expressive albeit stringly-typed darlings of Foundation.
There’s no doubt SwiftData brings forth many improvements. I won’t list them all here since this year’s WWDC videos cover them all. However, Core Data’s main problem was not a technical problem. It was a marketing problem!
Core Data by Tutorials is in its 8th edition and has continued to sell well for over a decade largely because Core Data intimidates developers. It has a reputation for having a steep learning curve and for being hard to use. Almost every time I’ve pitched it for a project, no one else wants to use it besides me, even in situations where having relational graph of objects made more sense. This knee-jerk reaction against Core Data is a shame because we’re talking about data persistence, something that should be table stakes for most applications.
So that’s the real challenge SwiftData has to overcome. Can it overcome the reputation of its predecessor? After watching all the SwiftData videos from this year’s WWDC, I think it can. In my experience, having a good or bad experience with Core Data is largely dependent on the scaffolding “glue” API you build around it so it can interact with your UI code. SwiftData was specifically designed to work with SwiftUI, which might seem strange at first since there’s nothing UI-specific about a persistence framework, but the benefit is that there’s almost no “glue” API to write because it’s all already written for you.
SwiftData also does away with the model file format (i.e. .xcdatamodeld), which is bad if you’re a visual learner, but great if you were confused by the code gen that was needed to create your model files. All in all, SwiftData seems, at first glance, to be more inviting to developers than Core Data ever was, and I expect it to have a much better adoption rate.