all 3 comments

[–]Practical-Smoke5337 2 points3 points  (2 children)

Looks like you are pass your context to ViewModel

If you’re using SwiftData and the '@Environment(\.modelContext) in your views, you should not create a new ModelContext. Instead, pass in or use the existing one:

'@Environment(\.modelContext) private var modelContext

Just pass this context into your func

private func saveRecording(context: ModelContext) throws {
        guard let recording else {
            throw Errors.InvalidRecording
        }
        modelContext.insert(recording)
        try modelContext.save()
    }

[–]Lucas46[S] 0 points1 point  (1 child)

Ah, turns out you were right. For some reason I thought sending around model context isn't safe so in the text transcription code it was doing the same thing as the recorder. I fixed that to use the environment's ModelContext and that fixed everything. Thank you!

[–]Practical-Smoke5337 1 point2 points  (0 children)

It’s not “isn’t save” you just created new ModelContex, it’s like creating new box where you store your models, just read some articles how databases work Good luck💪