So I'm trying to sum values from a ForEach list using the following code:
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(entity: Transaction.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Transaction.value, ascending: false)]) var fetchRequest: FetchedResults<Transaction>
var sum: Double {
fetchRequest.reduce(0) { $0 + $1.value } //ERROR!
}
Each time I run, there's an error:
[SwiftUI] Context in environment is not connected to a persistent store coordinator: <NSManagedObjectContext: 0x2813f2920>
USING PO:
(lldb) po fetchRequest
error: warning: couldn't get required object pointer (substituting NULL): Couldn't load 'self' because its value couldn't be evaluated
Here's the rest of the code:
var body: some View {
NavigationView {
Form {
...
Text("\(sum)")
}
}
...
Can you help me find the error please?