Core Data is one of Apple’s oldest frameworks. It’s a complex technology that allows you to persistently store data in a database. Database connections require thread-safety access and so does Core Data with so-called managed object contexts.
If you’re experienced with Core Data, you already know that it’s important to think about thread-safety access to managed objects. You can’t simply pass around a managed object from thread to thread. This and more, are concepts I want to dicuss in this module in relationship to Swift Concurrency.
Note
In this module, we’ll dive deeper into Swift Concurrency and Core Data. This is a less generic module, and some of you might not work with Core Data at all. If so, feel free to skip this module and continue ahead. This is also the reason why there isn’t an assessment for this module.
Core Data Best Practices
Before diving into the world of Swift Concurrency, I’d like to mention my repository, Core Data Best Practices briefly. It was part of a presentation I gave at conferences, such as NSSpain. Although the repository hasn’t been updated in a while, it still reflects many best practices that can be applied. Before migrating your project to Swift Concurrency, I think it’s smart first to make sure you’re using Core Data’s latest technologies, like persistent history tracking.
How about SwiftData?
I’m sure some of you wonder why I’m discussing Core Data when SwiftData is available to us. I expect many of you to be working on existing projects that have been ongoing for years. SwiftData is still relatively new and not yet widely adopted. At the same time, Apple is providing more guidance on SwiftData than it does for Core Data. I’ll likely add lessons for SwiftData later, but for now, I’d like to help out on Core Data first.
How well does Core Data work with Swift Concurrency?
Well, it depends! There are already asynchronous APIs available, like:
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension NSManagedObjectContext {
public func perform<T>(schedule: NSManagedObjectContext.ScheduledTaskType = .immediate, _ block: @escaping () throws -> T) async rethrows -> T
}
In other words, a method to perform work on the given managed object context by making use of async/await. At the same time, there’s no async alternative on NSPersistentContainer for a method we’re all using:
func loadPersistentStores(completionHandler block: @escaping (NSPersistentStoreDescription, (any Error)?) -> Void)
That describes the current state of Core Data’s adoptability in Swift Concurrency. We can benefit from some, but we will also have to define custom solutions.
Summary
Luckily, you’re in this dedicated course module that will help you move forward with Core Data and Swift Concurrency. Lessons in this module are based on my experience migrating RocketSim‘s Core Data stack to Strict Concurrency and Swift 6. At the same time, I would like to invite you to open lesson requests on the GitHub repository in case you’re still encountering challenges. I’ve tried my best to predict what’s needed, but it’s fair to say I’ll forget about common scenarios.
Enjoy this module!