title: How to setup a collectionView (or TableView) with NSFetchedResultsController and multiple fetched entities
body: <p>I have read here that the way to fetch multiple entities with 1 NSFetchedResultsController is to use a parent child inheritance model. I have such a model: </p>
  
  <p><a href="https://imgur.com/a/nckHzvr" rel="nofollow noreferrer">https://imgur.com/a/nckHzvr</a></p>
  
  <p>As you can see, TextPost,VideoPost, and ImagePost all have Skill as a parent entity. I am trying to make a single collectionView for which all three children show up. I am a little confused as to how to set the delegate methods though...</p>
  
  <p>Here is the code for the view controller</p>
  
  <pre><code>class Timeline2ViewController: UIViewController {
  
      @IBOutlet var postsCollectionView: UICollectionView!
      var skillName: String?
  
      fileprivate lazy var skillFetchedResultsController: NSFetchedResultsController&lt;Skill&gt; = {
          let appDelegate =
              UIApplication.shared.delegate as? AppDelegate
          let managedContext =
              appDelegate?.persistentContainer.viewContext
          let request: NSFetchRequest&lt;Skill&gt; = NSFetchRequest(entityName: "Skill")
          request.predicate = NSPredicate(format: "name == %@", self.skillName!)
          let timeSort = NSSortDescriptor(key: "timeStamp", ascending: true)
          request.sortDescriptors = [timeSort]
  
          let skillFetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: managedContext!, sectionNameKeyPath: nil, cacheName: nil)
          return skillFetchedResultsController
      }()
  
      override func viewDidLoad() {
          super.viewDidLoad()
          do {
              try skillFetchedResultsController.performFetch()
          } catch let error as NSError {
              print("SkillFetchError")
          }
      }
  }
  extension Timeline2ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          guard let sectionInfo = skillFetchedResultsController.sections?[section] else { return 0 }
          return sectionInfo.numberOfObjects
      }
  
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
          guard let cell = postsCollectionView.dequeueReusableCell(withReuseIdentifier: "pcell", for: indexPath) as? PostViewCell else { return fatalError("unexpected Index Path")
          }
          let post = skillFetchedResultsController[indexPath.row] /* This is the line im not sure about
          cell.background
          return cell
      }
  }
  </code></pre>
  
  <p>Since only 1 entity is actually, returned, I am not sure how to access an element at a specific index path. For instance skillFetchedResultsController[indexPath.row] would I think only have 1 entity - the skill itself. I really want to be accessing its children. Do I have to somehow subclass skillFetchedResultsController and return only the children Im interested in? </p>
  
  <p>Edit: with @pbasdf suggestions - I have this model:</p>
  
  <p><a href="https://i.stack.imgur.com/PQN3m.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PQN3m.png" alt="enter image description here"></a></p>
  
  <p>Now when I create an entity like so: </p>
  
  <pre><code>        guard let appDelegate =
           UIApplication.shared.delegate as? AppDelegate else {
               return
           }
           let managedContext = appDelegate.persistentContainer.viewContext
          let textPost = NSEntityDescription.insertNewObject(forEntityName: "TextPost", into: managedContext) as! TextPost
          textPost.text = "test text post"
          try! managedContext.save()
  </code></pre>
  
  <p>and I setup my fetched results controller to look at "Post2" like so:</p>
  
  <pre><code>    fileprivate lazy var skillFetchedResultsController: NSFetchedResultsController&lt;Post2&gt; = {
          let appDelegate =
              UIApplication.shared.delegate as? AppDelegate
          let managedContext =
              appDelegate?.persistentContainer.viewContext
          let request: NSFetchRequest&lt;Post2&gt; = NSFetchRequest(entityName: "Post2")
  //        request.predicate = NSPredicate(format: "skill = %@", self.skill!)
          let timeSort = NSSortDescriptor(key: "timeStamp", ascending: true)
          request.sortDescriptors = [timeSort]
          let skillFetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: managedContext!, sectionNameKeyPath: nil, cacheName: nil)
          return skillFetchedResultsController
      }()
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view.
  
          do {
              try skillFetchedResultsController.performFetch()
          } catch _ as NSError {
              print("SkillFetchError")
          }
  
      }
  </code></pre>
  
  <p>I see no returned results in:</p>
  
  <pre><code>func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      guard let sectionInfo = skillFetchedResultsController.sections?[section] else { return 0 }
      return sectionInfo.numberOfObjects
  }
  </code></pre>
  
  <p>Do I somehow need to link the two? Like create the Post object and the TextPost object at the same time? When I try and fetch TextPost objects directly it works. </p>