title: Swift UI - EnvironmentObject change does not update UI
body: <p>I´m banging my head against the desk about the following problem. I have a nested DataStore (for convenience), integrated into a view via @EnvironmentObject. If I change something in the DataStore (eg. putting a new object inside - which works fine-), the UI does not update. </p>
  
  <p>In the console, I also get the results of the print("Object Changed"), caused by a change in the fetched results. But the UI only updates after I restart the app. Whats wrong here?</p>
  
  <p>Thanks a lot in advance!</p>
  
  <p>My Test Code looks like this:</p>
  
  <p><strong>The DataStore:</strong></p>
  
  <pre><code>import Foundation
  import Combine
  
  class DataStore : ObservableObject{
      @Published var documents    = DocumentStore()
      @Published var attachments  = AttachmentStore()
  }
  
  </code></pre>
  
  <p><strong>The Attachment Store</strong></p>
  
  <pre><code>//
  //  AttachmentStore.swift
  
  import SwiftUI
  import Combine
  import CoreData
  
  class AttachmentStore: NSObject, ObservableObject {
  
  
      // MARK: Private Properties
       let persistenceManager = PersistenceManager()
  
      private lazy var fetchedResultsController: NSFetchedResultsController&lt;ManagedAttachment&gt; = {
          let fetchRequest: NSFetchRequest&lt;ManagedAttachment&gt; = ManagedAttachment.fetchRequest()
          fetchRequest.sortDescriptors = [NSSortDescriptor(key: "dateCreated", ascending: false)]
  
          let fetchedResultsController = NSFetchedResultsController(
              fetchRequest: fetchRequest,
              managedObjectContext: self.persistenceManager.managedObjectContext,
              sectionNameKeyPath: nil,
              cacheName: nil)
          fetchedResultsController.delegate = self
          return fetchedResultsController
      }()
  
      private var attachment: [ManagedAttachment] {
          return fetchedResultsController.fetchedObjects ?? []
      }
  
      // MARK: Public Properties
  
      @Published var Items : [ManagedAttachment] = []
  
      public var allItems: [ManagedAttachment] {
          return self.attachment.filter {$0 == $0}
         }
  
      func setup(){
          self.Items = allItems
      }
      // MARK: Object Lifecycle
  
      override init() {
          super.init()
          fetchDocuments()
          setup()
      }
  
      // MARK: Public Methods
  
      public func allAttachments(for id: UUID) -&gt; [ManagedAttachment]
      {
          let attachments = self.allItems.filter {$0.parentID == id}
          return attachments
      }
  
      public func create(attachment: Attachment) {
          ManagedAttachment.create(object: attachment, in: self.persistenceManager.managedObjectContext)
           saveChanges()
       }
  
      public func delete(attachment: ManagedAttachment){
          persistenceManager.managedObjectContext.delete(attachment)
          self.saveChanges()
      }
  
      public func update(){
          self.saveChanges()
      }
  
  
      // MARK: Private Methods
  
      private func fetchDocuments() {
          do {
              try fetchedResultsController.performFetch()
              dump(fetchedResultsController.sections)
          } catch {
              fatalError()
          }
      }
  
      private func saveChanges() {
          guard persistenceManager.managedObjectContext.hasChanges else { return }
          do {
              try persistenceManager.managedObjectContext.save()
              self.objectWillChange.send()
              print("Object Changed")
          } catch { fatalError() }
      }
  }
  
  
  extension AttachmentStore: NSFetchedResultsControllerDelegate {
      func controllerDidChangeContent(_ controller: NSFetchedResultsController&lt;NSFetchRequestResult&gt;) {
          self.objectWillChange.send()
          print("Object Changed")
      }
  }
  
  
  </code></pre>
  
  <p><strong>The TestView</strong></p>
  
  <pre><code>import SwiftUI
  
  struct TestView: View {
      @EnvironmentObject var data : DataStore
  
      var body: some View {
          VStack{
              Button(action: {
                  self.data.attachments.create(attachment: Attachment(url: "Test"))
              }
              ) {
                  Text("Add")
              }
              // Print the Items in the DataStore
              ForEach(data.attachments.Items, id:\.self){Item in
                  Text(Item.url).font(.caption)
              }
          }
  
      }
  }
  </code></pre>