I have a @Model
class like so:
@Model
final class MyModel {
@Attribute(.unique) var id: String
var firestoreStruct: FirestoreStruct
init(id: String, firestoreStruct: FirestoreStruct) {
self.id = id
self.firestoreStruct = firestoreStruct
}
}
FirestoreStruct
is a struct
with which I fetch data from Firestore:
struct FirestoreStruct: Codable {
@DocumentID var id: String?
var title: String
}
My use case is to save the FirestoreStruct
s to fetch them from Widgets etc (but I don’t even get there).
When I try to insert and save an instance of MyModel
to the modelContext
, I get an error
Fatal error: ‘try!’ expression unexpectedly raised an error:
FirebaseFirestore.FirestoreEncodingError.encodingIsNotSupported(“DocumentID
values can only be encoded with Firestore.Encoder”)
This is the code saving the data. However the error only gets produced when saving. When I remove the second line of code, it does postpone the crash to when minimizing the app (I assume SwiftData then tries to save it).
modelContext.insert(MyModel(id: "this is the ID", firestoreStruct: FirestoreStruct(title: "title")))
try modelContext.save()
I’ve set up the modelContainer correctly (as I believe)
.modelContainer(for: [MyModel.self])
How can I avoid this error?