title: How to update @FetchRequest, when a related Entity changes in SwiftUI?
body: <p>In a SwiftUI <code>View</code> i have a <code>List</code> based on <code>@FetchRequest</code> showing data of a <code>Primary</code> entity and the via relationship connected <code>Secondary</code> entity.
  The <code>View</code> and its <code>List</code> is updated correctly, when I add a new <code>Primary</code> entity with a new related secondary entity.</p>
  
  <p>The problem is, when I update the connected <code>Secondary</code> item in a detail view, the database gets updated, but the changes are not reflected in the <code>Primary</code> List.
  Obviously, the <code>@FetchRequest</code> does not get triggered by the changes in another View.</p>
  
  <p>When I add a new item in the primary view thereafter, the previously changed item gets finally updated.</p>
  
  <p>As a workaround, i additionally update an attribute of the <code>Primary</code> entity in the detail view and the changes propagate correctly to the <code>Primary</code> View.</p>
  
  <p>My question is:
  How can I force an update on all related <code>@FetchRequests</code> in SwiftUI Core Data?
  Especially, when I have no direct access to the related entities/<code>@Fetchrequests</code>?</p>
  
  <p><a href="https://i.stack.imgur.com/XKkJl.png" rel="noreferrer"><img src="https://i.stack.imgur.com/XKkJl.png" alt="Data Structure"></a></p>
  
  <pre><code>import SwiftUI
  
  extension Primary: Identifiable {}
  
  // Primary View
  
  struct PrimaryListView: View {
      @Environment(\.managedObjectContext) var context
  
      @FetchRequest(
          entity: Primary.entity(),
          sortDescriptors: [NSSortDescriptor(key: "primaryName", ascending: true)]
      )
      var fetchedResults: FetchedResults&lt;Primary&gt;
  
      var body: some View {
          List {
              ForEach(fetchedResults) { primary in
                  NavigationLink(destination: SecondaryView(primary: primary)) {
                  VStack(alignment: .leading) {
                      Text("\(primary.primaryName ?? "nil")")
                      Text("\(primary.secondary?.secondaryName ?? "nil")").font(.footnote).foregroundColor(.secondary)
                  }
                  }
              }
          }
          .navigationBarTitle("Primary List")
          .navigationBarItems(trailing:
              Button(action: {self.addNewPrimary()} ) {
                  Image(systemName: "plus")
              }
          )
      }
  
      private func addNewPrimary() {
          let newPrimary = Primary(context: context)
          newPrimary.primaryName = "Primary created at \(Date())"
          let newSecondary = Secondary(context: context)
          newSecondary.secondaryName = "Secondary built at \(Date())"
          newPrimary.secondary = newSecondary
          try? context.save()
      }
  }
  
  struct PrimaryListView_Previews: PreviewProvider {
      static var previews: some View {
          let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
  
          return NavigationView {
              PrimaryListView().environment(\.managedObjectContext, context)
          }
      }
  }
  
  // Detail View
  
  struct SecondaryView: View {
      @Environment(\.presentationMode) var presentationMode
  
      var primary: Primary
  
      @State private var newSecondaryName = ""
  
      var body: some View {
          VStack {
              TextField("Secondary name:", text: $newSecondaryName)
                  .textFieldStyle(RoundedBorderTextFieldStyle())
                  .padding()
                  .onAppear {self.newSecondaryName = self.primary.secondary?.secondaryName ?? "no name"}
              Button(action: {self.saveChanges()}) {
                  Text("Save")
              }
              .padding()
          }
      }
  
      private func saveChanges() {
          primary.secondary?.secondaryName = newSecondaryName
  
          // TODO: ❌ workaround to trigger update on primary @FetchRequest
          primary.managedObjectContext.refresh(primary, mergeChanges: true)
          // primary.primaryName = primary.primaryName
  
          try? primary.managedObjectContext?.save()
          presentationMode.wrappedValue.dismiss()
      }
  }
  </code></pre>