Observing changes to movie ratings

You're already observing the managed object context for changes, but they are only processed if the family member that is shown on the current page has updated. This logic should be replaced so that it will reload the table view if either the family member or their favorite movies change. Update the managedObjectContextDidChange(_:) method in MoviesViewController.swift as follows:

@objc func managedObjectContextDidChange(notification: NSNotification) {
  guard let userInfo = notification.userInfo
    else { return }

  if let updatedObjects = userInfo[NSUpdatedObjectsKey] as? Set<FamilyMember>,
    let familyMember = self.familyMember,
    updatedObjects.contains(familyMember) {

    tableView.reloadData()
  }

  if let updatedObjects = userInfo[NSUpdatedObjectsKey] as? Set<Movie> {
    for object in updatedObjects {
      if object.familyMember == familyMember {
        tableView.reloadData()
        break
      }
    }
  }
}
The logic for observing the family member hasn't changed; its conditions simply moved from the guard statement to an if statement. An extra if statement was added for the movies. If the updated object set is a list of movies, we loop through the movies and check whether one of the movies has the current family member as its family member. If so, the table is refreshed immediately and the loop is exited.

It's important that the loop in the second if statement is set up like this because you might have just added a movie for family member A and then switched to family member B while the new movie for family member A is still loading its rating. Also, breaking out of the loop early ensures that you don't loop over any more objects than needed. All you want to do is refresh the table view if one of the current family members' favorite movies is updated.

Okay, build and run your app to take it for a spin! You'll notice that everything works as you'd want it to right now. Adding new movies triggers a network request; as soon as it finishes, the UI is updated with the new rating. Sometimes, this update will be done in an instant, but it could take a short while if you have a slow internet connection. Great! That's it for this feature.