Post

Replies

Boosts

Views

Activity

Reply to iOS 18 SwiftData ModelContext reset
I have the same issue. However, for me it works find in the app, but fails intermittently when running my tests. // Allow Swift UI to observe changes to the container and update the UI in // response to changes. @Observable // To allow updates to the container to update the UI, the updates need to // happen on the main actor. @MainActor class DataContainer { let modelContainer: ModelContainer let myService: MyService var context: ModelContext { modelContainer.mainContext } init(inMemoryOnly: Bool = false, includeSampleData: Bool = false) { let schema = Schema([ MyModel.self, ]) let modelConfiguration = ModelConfiguration( schema: schema, isStoredInMemoryOnly: inMemoryOnly || includeSampleData, ) let context: ModelContext do { // Create the container. self.modelContainer = try ModelContainer( for: schema, configurations: [modelConfiguration], ) context = self.modelContainer.mainContext } catch { fatalError("Unable to create model container: \(error)") } self.myService = MyService(context: context) if includeSampleData { do { try self.loadSampleData() } catch { fatalError("Unable to create load sample data: \(error)") } } } } @MainActor @Suite("Test My Thing") struct MyThingTests { let dataContainer: DataContainer init() { self.dataContainer = DataContainer(inMemoryOnly: true) } func createItem(name: String) { let item = Item(name: name) self.dataContainer.context.insert(item) // <-- THIS LINE FAILS INTERMITTENLY } @Testfunc testThing() { self.createMeal(name: "Something") let result = self.service.doThing(item) #expect(result == true) } }
4d
Reply to iOS 18 SwiftData ModelContext reset
I have the same issue. However, for me it works find in the app, but fails intermittently when running my tests. // Allow Swift UI to observe changes to the container and update the UI in // response to changes. @Observable // To allow updates to the container to update the UI, the updates need to // happen on the main actor. @MainActor class DataContainer { let modelContainer: ModelContainer let myService: MyService var context: ModelContext { modelContainer.mainContext } init(inMemoryOnly: Bool = false, includeSampleData: Bool = false) { let schema = Schema([ MyModel.self, ]) let modelConfiguration = ModelConfiguration( schema: schema, isStoredInMemoryOnly: inMemoryOnly || includeSampleData, ) let context: ModelContext do { // Create the container. self.modelContainer = try ModelContainer( for: schema, configurations: [modelConfiguration], ) context = self.modelContainer.mainContext } catch { fatalError("Unable to create model container: \(error)") } self.myService = MyService(context: context) if includeSampleData { do { try self.loadSampleData() } catch { fatalError("Unable to create load sample data: \(error)") } } } } @MainActor @Suite("Test My Thing") struct MyThingTests { let dataContainer: DataContainer init() { self.dataContainer = DataContainer(inMemoryOnly: true) } func createItem(name: String) { let item = Item(name: name) self.dataContainer.context.insert(item) // <-- THIS LINE FAILS INTERMITTENLY } @Testfunc testThing() { self.createMeal(name: "Something") let result = self.service.doThing(item) #expect(result == true) } }
Replies
Boosts
Views
Activity
4d
Reply to Edit Team Name
There must be some sort of workaround no? What if i have a Typo?
Replies
Boosts
Views
Activity
1w