title: SwiftUI update view on core data object change
body: <p>The problem that I am having is that when I update a core data Asset object from a sheet view, the change is not reflected in the UI of the AssetListView. (Note that Inserting a new object from the sheet view <strong>does</strong> refresh the UI of the AssetListView. Deleting an object in sheet view <strong>also refreshes</strong> the UI of the AssetListView) <strong>The only action that is not working is the Update.</strong></p>
  <p>How can I make the AssetListView change when the core data object changes?</p>
  <p>I have the following SwiftUI code showing a list of assets from a CoreData FetchRequest:</p>
  <pre><code>struct AssetListView: View {
      @State private var showingSheet = false
      @State private var selectedAssetId: NSManagedObjectID?
      @Environment(\.managedObjectContext) var moc
      @FetchRequest(entity: Asset.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Asset.allocationPercentage, ascending: false)]) var assets: FetchedResults&lt;Asset&gt;
      
      var body: some View {
          VStack {
              Form {
                   ForEach(assets, id: \.self) { asset in
                      Section {
                          AssetRowView(asset: asset)
                              .onTapGesture {
                                  self.selectedAssetId = asset.objectID
                                  self.showingSheet = true
                          }
                      }
                  }
              }
              
          }
          .navigationBarTitle(&quot;Assets&quot;).sheet(isPresented: $showingSheet ) {
                  EditAssetView(assetId: self.selectedAssetId!)
                      .environment(\.managedObjectContext, self.moc)
                  }
          }
       }
  }
  </code></pre>
  <p>And this is an edit screen, which I present as SwiftUI sheet:</p>
  <pre><code>struct EditAssetView: View {
      var assetId: NSManagedObjectID
      @Environment(\.presentationMode) var presentationMode
      @State private var name = &quot;&quot;
      @State private var description = &quot;&quot;
      @Environment(\.managedObjectContext) var moc
      
      var asset: Asset {
          moc.object(with: assetId) as! Asset
      }
     
      var body: some View {
          NavigationView {
              Form {
                  Section {
                      TextField(&quot;Name&quot;, text: $name)
                      TextField(&quot;Description&quot;, text: $description)
                  }
              }
              .navigationBarTitle(Text(&quot;Edit Asset&quot;), displayMode: .inline)
              .navigationBarItems(leading: Button(&quot;Cancel&quot;) {
                  self.presentationMode.wrappedValue.dismiss()
                  }, trailing: Button(&quot;Done&quot;) {
                      self.presentationMode.wrappedValue.dismiss()
                      self.asset.name = self.name
                      self.asset.assetDescription = self.description
                      try? self.moc.save()
                  }
              )
          }
          .onAppear {
              self.name = self.asset.name
              self.description = self.asset.assetDescription
          }
      }
  }
  </code></pre>
  <p>Here is the code for AssetRowView:</p>
  <pre><code>struct AssetRowView: View {
      var asset: Asset?
      
      var body: some View {
          HStack {
             Text(asset.name)
             Text(asset.assetDescription)
          }
      }
  }
  </code></pre>