1

I am working on a personal app to help me log income and calculate tax etc

I have a view that displays the totals and does a little math to calculate tax and profits etc. To get the sum values I use the following function:

func sumRevenue() -> Double {
var revenueTotal : Double = 0.00

let expression = NSExpressionDescription()
expression.expression =  NSExpression(forFunction: "sum:", arguments:[NSExpression(forKeyPath: "revenue")])
expression.name = "revenueTotal";
expression.expressionResultType = NSAttributeType.doubleAttributeType

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Log")
fetchRequest.propertiesToFetch = [expression]
fetchRequest.resultType = NSFetchRequestResultType.dictionaryResultType

do {
    let results = try managedObjectContext.fetch(fetchRequest)
    let resultMap = results[0] as! [String:Double]
    revenueTotal = resultMap["revenueTotal"]!
} catch let error as NSError {
    NSLog("Error when summing amounts: \(error.localizedDescription)")
}

return revenueTotal
}

In my CoreData model, "Log" is the entity and in this case, "revenue" would be the attribute. All works well and the totals display in the TotalsView by using "sumRevenue", however the totals don't update in that View when I add a new log, but they are added to the list of logs just fine and do update if I rebuild.

I know the answer probably has something to do with ObservedObject but I am new to all this and I am struggling with it. Any advice would be much appreciated.

Nazaroth
  • 11
  • 2
  • 1
    Welcome to Stack Overflow! Please take the [tour](https://stackoverflow.com/tour) and see: [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Reproducible Example (MRE)](https://stackoverflow.com/help/minimal-reproducible-example). CoreData can be hard to debug in particular. We really need an MRE. Also, screenshotting the CoreData Model will help as well. But you need to make a new, simplified app that has the same problem as you are seeing for debugging. – Yrb May 25 '22 at 17:42
  • The answer has to do with `ObservedObject`, because Core Data entities are `ObservableObjects`. But without some sample code of your view, it's hard to guess why you're finding issues. – HunterLion May 25 '22 at 17:53
  • The problem is you don't know when to re-fetch the sum if a new Log entry is added. Derived attributes are designed to help with this, e.g. a sum attribute on an entity that is the parent of all the Logs, it's updated when a new Log is added so you can perhaps use a @FetchRequest on the parent entity to receive updates. https://developer.apple.com/documentation/coredata/nsderivedattributedescription – malhal May 25 '22 at 19:52
  • @malhal I am not sure what I have done but the totalsview now updates when a new log is added/removed. All I did was remove the views from the tab bar as they are redundant since I added a summary card view to the contentview. The card has a nav link to the totals view. However the card does not update. I will try and post a screen recording if that helps? I will also add some view code to my original post – Nazaroth May 26 '22 at 13:48

0 Answers0