While reading the developer documentation article Adopting SwiftData for a Core Data App, one particular line piqued my interest.
For apps that evolve from a version that doesn’t have any app group container to a version that has one, SwiftData copies the existing store to the app group container.
Given how troublesome it has been to migrate the Core Data persistent store to an app group container, I decided to try this out myself. I created an Xcode project using the default Core Data template. I then added a few Item
objects with timestamps. There, I had what we would consider a regular Core Data app.
I then created a widget extension for this app since this is one of the most common uses for adopting an app group in an Xcode project. After that, I linked the main target with the widget extension using an app group. In the widget extension, I tried to fetch the Item
objects. I utilized the SwiftData code in the sample project associated with the article above.
struct Provider: TimelineProvider {
private let modelContainer: ModelContainer
init() {
let appGroupContainerID = "group.com.genebogdanovich.CoreDataSwiftDataAppGroup"
guard let appGroupContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupContainerID) else {
fatalError("Shared file container could not be created.")
}
let url = appGroupContainer.appendingPathComponent("CoreDataSwiftDataAppGroup.sqlite")
print("\(url)")
do {
modelContainer = try ModelContainer(for: Item.self, configurations: ModelConfiguration(url: url))
} catch {
fatalError("Failed to create the model container: \(error)")
}
}
}
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
Task { @MainActor in
let fetchDescriptor = FetchDescriptor<Item>()
let items: [Item] = try! modelContainer.mainContext.fetch(fetchDescriptor)
print(items)
let entry = SimpleEntry(date: .now, emoji: "😀", count: items.count)
let timeline = Timeline(entries: [entry], policy: .never)
completion(timeline)
}
}
The fetch yielded no results. However, as I explored the app group directory in the file system, I found a .sqlite
file. That is interesting because SwiftData creates .store
files by default. So, I am guessing that SwiftData did copy something. Or the ModelContainer
initializer just created another empty SQLite file since the fetch returned zero results.
I would highly appreciate someone elaborating on that quote from the documentation.