title: SwiftUI: Run code whenever FetchedResults changes
body: <p>I'm new to Core Data and struggling to find a simple solution to the below scenario.</p>
  <p>I have a view like this:</p>
  <pre><code>struct SomeView: View {
  @Environment(\.managedObjectContext) private var viewContext
  
  @FetchRequest(
      sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],
      animation: .default)
  
  private var items: FetchedResults&lt;Item&gt;
  
  
  var body: some View {
      List {
          ForEach(items) { item in
              // Render item view
          }
      }
  }
  </code></pre>
  <p>}</p>
  <p>And I want to run an arbitrary handler whenever <code>Items</code> has changed. The list view updates as I expect, but I'm not sure how I should trigger non-ui code (say to send an API request every time Items changes). What is the simplest way to do this?</p>
  <p>Searching around I'm seeing answers to similar questions point to <code>NSFetchedResultsController</code> and articles like <a href="https://cargath.github.io/blog/2019/06/25/SwiftUI-NSFetchedResultsController" rel="nofollow noreferrer">this</a>, but I haven't seen a clear example of how to implement exactly what I want. If the answer truly is <code>NSFetchedResultsController</code>, could someone show me complete code to get this working?</p>
  <p>EDIT: I seem to be able to achieve what I want by adding something like <code>let _ = handleItemsChanged(items: items)</code> inside the list view, but this seems hacky. Any cleaner mechanisms that are equally simple?</p>