I have had some success by implementing manual saves using modelContext.transaction() whenever interacting with the model.
Post
Replies
Boosts
Views
Activity
I think I have made some progress with this by introducing manual saves when working on the model, so for example, instead of:
selectedItem = item
I have replaced with:
do {
try modelContext.transaction {
selectedItem = item
} catch {
print("unable to save change due to error \(error)")
}
Items are now successfully persisting, and the error:
SwiftData/ModelContext.swift:3253: Fatal error: Failed to identify a store that can hold instances of SwiftData.
Seems to have gone away.
It seems like the error was something to do with the way that changes are stashed and saved in the background by SwiftData, I am not an expert but my best guess is that it is related to the recent changes in concurrency architecture.
I have also found SwiftData to be much more reliable if you work directly on elements derived from @Query in the view, rather than trying to save on @Bindable from a parent. I pass the UUID around the views and then filter the arrray from @Query for the id.
I have a SwiftData document-based app with iCloud documents enabled, and lots of one-to-many @model relationships.
I have the same issue as reported, and I am getting the error:
Publishing changes from background threads is not allowed; make sure to publish values from the main thread
I also get a crash shortly after with the breakpoint message:
SwiftData/ModelContext.swift:3253: Fatal error: Failed to identify a store that can hold instances of SwiftData.
I wonder if the two errors are related.
I have not tested on the simulator, but have tested on iPad Pro M1 and iPhone 12
Both errors have only appeared in ios18, and the app has been running great for many months in ios17 with no crashes. Suddenly ios18 is released and the entire app is broken. Grrrrr.
I have the same issue. Has anyone found a workaround?
I have the same issue, it has completely broken my project. I have upgraded to Swift 6, but that didnt help. I have tried manually creating a container, but the container is not unique to the document.
I have a large data model with many relationships, it stores simple data and image data. It has been working great for a year, and then completely crashes on IOS18!
I am at a loss, please help!
You can drag a .png file straight into the little window that says "Add exported type identifier...", how to get your app to actually display these is a different matter....
My issue is now fixed. It was that I had not deployed schema changes from development to production containers in the CloudKit console. The cause of logging out intermittently might be a response to finding an inconsistency between the data model and container.
From the release notes:
Known Issues: Apps using SwiftData that are built with Xcode 15 beta 6 have known issues when running on iOS 17 beta 6. (113915428): Workaround: Use iOS 17 Beta 5 with Xcode Beta 6.
Correct me if I am wrong, but I dont think we can easily downgrade in IOS, so I will be waiting for Beta 7 of Xcode.
It seems that you can make all of the variables optional where they are declared in the @Model class (eg var variable: String?), and then use nil coalescing to unwrap when the properties are called (?? ""), but then you get a compile time error when you call the parameter from @Bindable in the View using a binding. The below will not compile, even with the ?? "".
struct egView: View {
@Bindable var VM: ViewModel
var body: some View {
ButtonStruct(Variable: $VM.variable ?? "" )
}
Performing nil coalescing in the ButtonStruct doesnt seem to work either, so I am stumped.
I had the same issue after the XCode beta 5 update.
My issue has three models in an array, so a slightly different solution:
import SwiftData
import SwiftUI
@main
struct SurvAIApp: App {
let modelContainer: ModelContainer
init() {
do {
modelContainer = try ModelContainer(for: [ViewModel1.self,ViewModel2.self, ViewModel3.self])
} catch {
fatalError("Could not initialize ModelContainer")
}
}
var body: some Scene {
WindowGroup {
NavigationStack {
ContentView()
}
.modelContainer(modelContainer )
}
}