Hi all,
I was examining an app I made a couple of months ago and it now crashes with the error This model instance was invalidated because its backing data could no longer be found the store.
whenever I open a recording. I manually save my model into the model context as so:
private func saveRecording(modelContainer: ModelContainer) throws {
let modelContext = ModelContext(modelContainer)
guard let recording else {
throw Errors.InvalidRecording
}
modelContext.insert(recording)
try modelContext.save()
}
As well, I also use a query to fetch recordings like I’m supposed to:
init(searchString: String) {
self.searchString = searchString
_recordings = Query(filter: searchString.isEmpty ? nil : #Predicate<Recording> { recording in
recording.name?.localizedStandardContains(searchString) ?? false
}, sort: [SortDescriptor(\Recording.date, order: .reverse)])
}
Could it be how I’m using creating the model context in the saveRecording
function? If you need any more code, feel free to ask. My repo is here: GitHub - aabagdi/MemoMan. Thank you for any help!
1 Like
Welcome to the community.
Disregard this post. See below.
Update.
I downloaded your project and since I am using Xcode 16.2 it asked me to update the code to the latest settings. I complied the code and ran it on my device which is running iOS 18.4. It ran no problem so disregard the suggestions in my previous post. Obviously I should have downloaded the App in the first instance and tried it.
That’s interesting that it works for you, I’m on iOS 18.5 beta and the app still crashes for me. I tried it on an 18.4 simulator as well and it still didn’t work. I also tried downloading from the App Store and still it crashes. How did you update the settings?
EDIT:
I fixed it! I just changed the code to use the environment’s Model Context instead of creating a new one each time.
That’s what I thought that you needed to do initially and that’s why I posted those changes in my initial post. I did get a crash with the same message that you did but could not replicate it so changed the code back to what you had and then noticed the message to update the internal settings (due to iOS changes I assume) and it worked. It makes more sense to me to use the Environment modelContext in that it is the single source of truth. Good to hear that you have got it working but I’m still not sure what the real issue is (was) and why I had that single crash on playback that I was not able to replicate. Did you remove and reinstall the App on your real device each time?
I tried uninstalling and reinstalling a couple of times, but still got the same error.
Sounds like this might have something to do with 18.5 beta more than anything else. It works on my 18.4 simulator and my 18.4 real device.
Have you tried with an 18.4 simulator?
Yep, I tried it on an 18.4 simulator and still got the same crash. This is very strange that you aren’t able to reproduce it.
Are you running a Beta version of Xcode as well? As I said in one of my earlier posts I am using Xcode 16.2
I’m running the release version of Xcode 16.3.
I shall download the latest release and see what happens.
Hey, I ran into a similar issue after updating to iOS 18… Swiftdata seems to be acting a little weird in certain cases since the update.
In my case, I had a model that worked fine before, but after the update, it started throwing unexpected errors during saving. What helped me was double-checking the model’s annotations and making sure all properties had types that are currently supported by SwiftData. Also, make sure you’re not using any computed properties or optionals in a way that SwiftData might not like.
If you’re still stuck, try commenting out parts of your model and adding them back one by one … sounds tedious, but it helped me pinpoint where things were breaking.
Hope that helps a bit! Would be curious to know what the exact error is… might help narrow it down further.