title: Refresh values in a view that displays the sum of an attribute from CoreData in SwiftUI
body: <p>I am working on a personal app to help me log income and calculate tax etc</p>
  <p>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:</p>
  <pre><code>func sumRevenue() -&gt; Double {
  var revenueTotal : Double = 0.00
  
  let expression = NSExpressionDescription()
  expression.expression =  NSExpression(forFunction: &quot;sum:&quot;, arguments:[NSExpression(forKeyPath: &quot;revenue&quot;)])
  expression.name = &quot;revenueTotal&quot;;
  expression.expressionResultType = NSAttributeType.doubleAttributeType
  
  let fetchRequest = NSFetchRequest&lt;NSFetchRequestResult&gt;(entityName: &quot;Log&quot;)
  fetchRequest.propertiesToFetch = [expression]
  fetchRequest.resultType = NSFetchRequestResultType.dictionaryResultType
  
  do {
      let results = try managedObjectContext.fetch(fetchRequest)
      let resultMap = results[0] as! [String:Double]
      revenueTotal = resultMap[&quot;revenueTotal&quot;]!
  } catch let error as NSError {
      NSLog(&quot;Error when summing amounts: \(error.localizedDescription)&quot;)
  }
  
  return revenueTotal
  }
  </code></pre>
  <p>In my CoreData model, &quot;Log&quot; is the entity and in this case, &quot;revenue&quot; would be the attribute. All works well and the totals display in the TotalsView by using &quot;sumRevenue&quot;, 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.</p>
  <p>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.</p>