ARTICLE AD BOX
I am struggling to resolve a runtime "error" when trying to load model data with SwiftData in my unit tests.
I use the makeModelContext function on the MainActor to create an in-memory model context:
/// Create an in-memory ModelContext containing all app models for testing. /// This mirrors the models registered in the App's ModelContainer. @MainActor static func makeModelContext() throws -> ModelContext { // Use an in-memory model configuration for tests to avoid CloudKit/SwiftData background // registration collisions with the app's shared ModelContainer. let config = ModelConfiguration(isStoredInMemoryOnly: true) let schema = Schema([ Car.self, Charger.self, ChargingCostPlan.self, ChargingSession.self, ChargingSessionTemplate.self, PriceElement.self, Location.self, HomeConsumption.self ]) let container = try ModelContainer(for: schema, configurations: [config]) // Disable autosave to avoid background persistence during tests container.mainContext.autosaveEnabled = false return container.mainContext }In my test function, I call the makeModelContext function, also from the MainActor:
@Test("Import a representative subset of the backup JSON") @MainActor func testImportRepresentativeSubset() async throws { // Locate the test data JSON next to this test file let testFileURL = URL(fileURLWithPath: #file).deletingLastPathComponent().appendingPathComponent("TestData_2026-03-07.json") let raw = try Data(contentsOf: testFileURL) let decoder = JSONDecoder() decoder.dateDecodingStrategy = .iso8601 let backup = try decoder.decode(DataEncoder.Backup.self, from: raw) let encoder = JSONEncoder() encoder.outputFormatting = [.prettyPrinted, .sortedKeys] encoder.dateEncodingStrategy = .iso8601 let data = try encoder.encode(backup) // Create an in-memory ModelContext (helper provided by test infra) let modelContext = try TestHelpers.makeModelContext() modelContext.autosaveEnabled = false // Import let report = try DataImporter.importFrom(data: data, into: modelContext) ...In the last line of above code, I pass this modelContext from the test function into the function to be tested:
/// Import from raw JSON Data (the shape produced by `DataEncoder.Backup`). /// - Parameters: /// - data: JSON data produced by `DataEncoder.export` /// - modelContext: the SwiftData `ModelContext` to insert objects into /// - Returns: an import report summarising the operation. @MainActor public static func importFrom(data: Data, into modelContext: ModelContext) throws -> DataImportReport { var report = DataImportReport() let decoder = JSONDecoder() decoder.dateDecodingStrategy = .iso8601 do { let backup = try decoder.decode(DataEncoder.Backup.self, from: data) // Prefetch existing objects once to avoid repeated fetches and actor issues let existingCars: [Car] = (try? modelContext.fetch(FetchDescriptor<Car>())) ?? [] ...In the last line of above code, I get the following runtime message and the testing process stops:
Task 55: EXC_BREAKPOINT (code=1, subcode=0x1d9d2a8b4)
I tried to find solutions via web search, Github Copilot and Xcode Coding Assistant, but none of the proposals helped. I checked the following things:
Model registration done
modelContext is valid
Predicate is valid
Any suggestions what could be the root cause?
