title: Background context save causes NSFetchedResultsController didChange anObject to call .delete instead of .update
body: <p>I'm using an NSFetchedResultsController in a UICollectionView. </p>
  
  <p><strong>The problem</strong>: When the user opens the view I send a request to the API and parse the response in the background with <code>performBackgroundTask()</code>. The NSFetchedResultsController will load the Managed Objects correctly, but after I parse the data and call <code>context.save()</code> the <code>didChangeObject</code> method is called with <code>NSFetchedResultsChangeType.delete</code> but neither <code>update</code> nor <code>insert</code> are called after that.</p>
  
  <p>Here's some code.</p>
  
  <p><strong>Parsing and Saving</strong></p>
  
  <pre><code>static func parse(withJSON json: [JSON]) {
      let container = (UIApplication.shared.delegate as! AppDelegate).persistentContainer
  
      container.performBackgroundTask() { (context) in
          context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
  
          for feedJSON in json {
              if let feedID = feedJSON["_id"].string {
                  let feed = Feed.getWithId(id: feedID, withContext: context) ?? (NSEntityDescription.insertNewObject(forEntityName: "Feed", into: context)  as! Feed)
                  feed.update(withJSON: feedJSON, withContext: context)
              }
          }
  
          do {
              try context.save()
          } catch {
              fatalError("Failure to save context: \(error)")
          }
      }
  }
  </code></pre>
  
  <p><strong>View Context</strong></p>
  
  <pre><code>lazy var persistentContainer: NSPersistentContainer = {        
      let container = NSPersistentContainer(name: "TheApp")
      container.loadPersistentStores(completionHandler: { (storeDescription, error) in
  
          if let error = error as NSError? {                
              #if DEVELOPMENT
                  fatalError("Unresolved error \(error), \(error.userInfo)")
              #endif
          }
      })
  
      container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
      container.viewContext.automaticallyMergesChangesFromParent = true
      container.viewContext.retainsRegisteredObjects = true
  
      return container
  }()
  </code></pre>
  
  <p><strong>Initializing the NSFetchedResultsController</strong></p>
  
  <pre><code>func initializeFetchedResultsController() {
      guard let user = Current.user else {
          return
      }
  
      let request: NSFetchRequest&lt;Feed&gt; = Feed.fetchRequest()
      let createdAtSort = NSSortDescriptor(key: "createdAt", ascending: false)
      request.sortDescriptors = [createdAtSort]
      request.predicate = NSPredicate(format: "author = %@", user)
  
      let moc = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
  
      resourceFetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: moc, sectionNameKeyPath: nil, cacheName: nil)
      resourceFetchedResultsController?.delegate = self
  
      do {
          try resourceFetchedResultsController?.performFetch()
      } catch {
          fatalError("Failed to initialize FetchedResultsController: \(error)")
      }
  }
  </code></pre>
  
  <p><code>author</code> is a to-one relationship to a <code>User</code> managed object. I've verified that this isn't somehow being deleted when the background context is saved. I've also changed the Delete Rule for the Feed.author relationship to <code>No Action</code>, <code>Nullify</code>, and <code>Cascade</code> with no change.</p>
  
  <p><strong>Object Change Notifications</strong></p>
  
  <p>Finally, I've set notifications to listen to changes in the context. It is successfully called after every <code>context.save()</code> in the <code>performBackgroundTask()</code>, but it only ever has information on the object being updated, and never deleted.</p>
  
  <pre><code>NotificationCenter.default.addObserver(self, selector: #selector(managedObjectContextObjectsDidChange), name: NSNotification.Name.NSManagedObjectContextObjectsDidChange, object: nil)
  
  NotificationCenter.default.addObserver(self, selector: #selector(managedObjectContextObjectsDidChange), name: NSNotification.Name.NSManagedObjectContextWillSave, object: nil)
  
  NotificationCenter.default.addObserver(self, selector: #selector(managedObjectContextObjectsDidChange), name: NSNotification.Name.NSManagedObjectContextDidSave
  </code></pre>
  
  <p><strong>The one things that did work...</strong></p>
  
  <p>When I change the predicate from <code>NSPredicate(format: "author = %@", user)</code> to <code>NSPredicate(format: "title = %@", 'My Title')</code> it will work as expected. Of course, this only works if for the one object with that title, but it will successfully save the context, merge changes, and update the collection as I would expect. </p>