I have an existing app that uses SwiftData and now want to add widgets. I added the widget extension, created an App Group to use for the main app target and widget targets and successfully created the widget. However, when testing the updates I often experience data loss - as though the including the widget extension is creating a new instance of modelContainer. Am I missing something to ensure there won't be any data loss when adding the App Group and widget extension?
For additional context: I’ve followed the Backyard Birds example code except that it uses a separate app package. My app does not use an external app package, but I am using some elements of the DataGeneration file. My files containing the SwiftData models have Target Memberships for both the main app target and widget extension target.
In the TimelineProvider for my widgets, I'm doing the following:
let modelContext = ModelContext(DataGeneration.container)
init() {
DataGeneration.generateAllData(modelContext: modelContext)
}
My DataGeneration file (simplified) is as follows. When adding the widget target, I sometimes see the log for "Creating instance of DataGeneration".
import Foundation
import SwiftData
@Model
class DataGeneration {
var requiresInitialization: Bool = true
init(requiresInitialization: Bool = true) {
self.requiresInitialization = requiresInitialization
}
private func generateInitialData(modelContext: ModelContext) {
if requiresInitialization {
let budget = Budget()
modelContext.insert(budget)
requiresInitialization = false
}
}
private static func instance(with modelContext: ModelContext) -> DataGeneration {
if let result = try! modelContext.fetch(FetchDescriptor<DataGeneration>()).first {
logger.info("Found instance of DataGeneration")
return result
} else {
logger.info("Creating instance of DataGeneration")
let instance = DataGeneration()
modelContext.insert(instance)
return instance
}
}
static func generateAllData(modelContext: ModelContext) {
let instance = instance(with: modelContext)
instance.generateInitialData(modelContext: modelContext)
}
}
extension DataGeneration {
static let container = try! ModelContainer(for: schema, configurations: [.init(isStoredInMemoryOnly: DataGenerationOptions.inMemoryPersistence)])
static let schema = SwiftData.Schema([
DataGeneration.self,
Budget.self
])
}
4
0
254