I'm developing a SwiftUI, CoreData / CloudKit App with Xcode 16.2 & Sequoia 15.2. The main CoreData entity has 20 NSManaged vars and 5 derived vars, the latter being computed from the managed vars and dependent computed vars - this somewhat akin to a spreadsheet. The data entry/updating Form is complex for each managed var because the user needs to be able to enter data in either Imperial or Metric units, and then switch (by Button) between units for viewing equivalents. This has to happen on an individual managed var basis, because some source data are in Imperial and others Metric.
Consequently, I use a generalised SubView on the Form for processing the managed var (passed as a parameter with its identity) and then updating the CoreData Entity (about 100 lines of code in total): i.e. there are 20 uses of the generalised SubView within the Main Form. However, none of the SubViews triggers an update of the managed var in the Form, nor computations of the derived vars. On initial load of the app, the CoreData entity is retrieved and the computations happen correctly: thereafter not.
No technique for refreshing either View works: e.g. trigger based on NSManagedObjectContextDidSave; nor does reloading the CoreData entity after Context Save (CoreData doesn't recognise changes at the attribute level anyway). If the SubView is removed and replaced with code within the Form View itself, then it works. However, this will require about 40 @State vars, 20 onCommits, etc - so it's not something I'm keen to do.
Below is a much-simplified example of the problem.
Form{
Section(header: Text("Test Record")){
Text(testRec.dateString ?? "n/a")
TextField("Notes",text:$text)
.onSubmit{
testRec.notes = text
dataStore.contextSave()
}
//DoubleNumberEntry(testRec: testRec) - doesn't work
TextField("Double",value:$numDbl,format: .number)
// This works
.onSubmit{
testRec.dblNum = numDbl
dataStore.contextSave()
}
TextField("Integer",value: $numInt,format: .number)
.onSubmit{
testRec.intNum = Int16(numInt)
dataStore.contextSave()
}
Text(String(format:"%.2f",testRec.computation))
Section(header: Text("Computations")) {
Text(String(format:"%.2f",testRec.computation))
Text(String(format:"%.2f",testRec.anotherComputation))
}
}
}
A much simplified version of my NSManaged var entry/updating.
struct DoubleNumberEntry: View {
let dataStore = MainController.shared.dataStore
var testRec : TestRec
@State private var numDbl: Double
init(testRec: TestRec) {
self.testRec = testRec
self.numDbl = testRec.dblNum
}
var body: some View {
TextField("Double",value:$numDbl,format: .number)
.onSubmit{
testRec.dblNum = numDbl
dataStore.contextSave()
}
}
}
I'd appreciate any offered solution or advice.
Regards, Michaela
UPDATE: The problem with this sample (Test) code was with the way I passed the CoreData Entity to the Form.
My in-development app updates the NSManaged vars correctly and recomputes the derived data, but there's a problem (yet to be resolved) in getting all those back into the Form for display, something to do with the way I've generalised the Form's processing.
Apologies for any inconvenience.
Regards, Michaela