I just started using CoreData with SwiftUI.
After following this series of tutorial.
And I am facing a situation where I cannot save data as I expect. Here is the relevant code:
struct MyView: View {
......
@Environment(\.managedObjectContext) var managedObjectContext
func setData() {
print(#function)
.....
let myData = SomeEntity(context: self.managedObjectContext)
myData.name = "myName"
myData......
do {
try self.managedObjectContext.save()
} catch let error {
// Handle the Core Data error.
print("Can't save as expected!!!!")
print("Error : \(error)")
}
}
.....
var body: some View {
........
}
}
When this code executes I get this error:
Can't save as expected!!!!
Error : nilError
Can somebody tell me what I need to check?
I am all the more puzzled, that I have another part in my app (apparently similar), in a view one level above, where the saving is perfectly working.
In case this may be useful, the view is presented with code like this:
}).sheet(isPresented: $showingFlag) {
MyView(.....)
}
In the presenting view, data saving is working.