title: How to ensure WidgetKit view shows correct results from @FetchRequest?
body: <p>I have an app that uses Core Data with CloudKit. Changes are synced between devices. The main target has Background Modes capability with checked Remote notifications. Main target and widget target both have the same App Group, and both have iCloud capability with Services set to CloudKit and same container in Containers checked.</p>
  <p>My goal is to display <em>actual</em> Core Data entries in SwiftUI WidgetKit view.</p>
  <p>My widget target file:</p>
  <pre><code>import WidgetKit
  import SwiftUI
  import CoreData
  
  // MARK: For Core Data
  
  public extension URL {
      /// Returns a URL for the given app group and database pointing to the sqlite database.
      static func storeURL(for appGroup: String, databaseName: String) -&gt; URL {
          guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup) else {
              fatalError(&quot;Shared file container could not be created.&quot;)
          }
          
          return fileContainer.appendingPathComponent(&quot;\(databaseName).sqlite&quot;)
      }
  }
  
  var managedObjectContext: NSManagedObjectContext {
      return persistentContainer.viewContext
  }
  
  var workingContext: NSManagedObjectContext {
      let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
      context.parent = managedObjectContext
      return context
  }
  
  var persistentContainer: NSPersistentCloudKitContainer = {
      let container = NSPersistentCloudKitContainer(name: &quot;Countdowns&quot;)
      
      let storeURL = URL.storeURL(for: &quot;group.app-group-countdowns&quot;, databaseName: &quot;Countdowns&quot;)
      let description = NSPersistentStoreDescription(url: storeURL)
      
      
      container.loadPersistentStores(completionHandler: { storeDescription, error in
          if let error = error as NSError? {
              print(error)
          }
      })
          
      container.viewContext.automaticallyMergesChangesFromParent = true
      container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
      
      return container
  }()
  
  // MARK: For Widget
  
  struct Provider: TimelineProvider {
      var moc = managedObjectContext
      
      init(context : NSManagedObjectContext) {
          self.moc = context
      }
      
      func placeholder(in context: Context) -&gt; SimpleEntry {
          return SimpleEntry(date: Date())
      }
      
      func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -&gt; ()) {
          let entry = SimpleEntry(date: Date())
          return completion(entry)
      }
      
      func getTimeline(in context: Context, completion: @escaping (Timeline&lt;Entry&gt;) -&gt; ()) {
          var entries: [SimpleEntry] = []
          
          let currentDate = Date()
          for hourOffset in 0 ..&lt; 5 {
              let entryDate = Calendar.current.date(byAdding: .minute, value: hourOffset, to: currentDate)!
              let entry = SimpleEntry(date: entryDate)
              entries.append(entry)
          }
          
          let timeline = Timeline(entries: entries, policy: .atEnd)
          completion(timeline)
      }
  }
  
  struct SimpleEntry: TimelineEntry {
      let date: Date
  }
  
  
  struct CountdownsWidgetEntryView : View {
      var entry: Provider.Entry
      
      @FetchRequest(entity: Countdown.entity(), sortDescriptors: []) var countdowns: FetchedResults&lt;Countdown&gt;
      
      var body: some View {
          return (
              VStack {
                  ForEach(countdowns, id: \.self) { (memoryItem: Countdown) in
                      Text(memoryItem.title ?? &quot;Default title&quot;)
                  }.environment(\.managedObjectContext, managedObjectContext)
                  Text(entry.date, style: .time)
              }
          )
      }
  }
  
  @main
  struct CountdownsWidget: Widget {
      let kind: String = &quot;CountdownsWidget&quot;
      
      var body: some WidgetConfiguration {
          StaticConfiguration(kind: kind, provider: Provider(context: managedObjectContext)) { entry in
              CountdownsWidgetEntryView(entry: entry)
                  .environment(\.managedObjectContext, managedObjectContext)
          }
          .configurationDisplayName(&quot;My Widget&quot;)
          .description(&quot;This is an example widget.&quot;)
      }
  }
  
  struct CountdownsWidget_Previews: PreviewProvider {
      static var previews: some View {
          CountdownsWidgetEntryView(entry: SimpleEntry(date: Date()))
              .previewContext(WidgetPreviewContext(family: .systemSmall))
      }
  }
  </code></pre>
  <p><strong>But I have a problem:</strong> let's say I have 3 <code>Countdown</code> records in the main app:</p>
  <p>At the start widget view shows 3 records as expected in preview (UI for adding a widget). But after I add a widget to the home screen, it does not show <code>Countdown</code> rows, only <code>entry.date, style: .time</code>. When timeline entry changes, rows not visible, too. I made a picture to illustrate this better:</p>
  <p><a href="https://i.stack.imgur.com/r7OyG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/r7OyG.png" alt="adding a widget" /></a></p>
  <p>Or:</p>
  <p>At the start widget view shows 3 records as expected, but after a minute or so, if I delete or add <code>Countdown</code> records in the main app, widget <em>still</em> shows initial 3 values, but I want it to show the actual number of values (to reflect changes). Timeline <code>entry.date, style .time</code> changes, reflected in the widget, but not entries from request.</p>
  <p>Is there any way to ensure my widget shows correct fetch request results? Thanks.</p>