title: How to make a Realm collection observer section aware like `NSFetchedResultsController`?
body: <p>I’m new to Realm and I’m hoping it can do something that is easy in CoreData. First, I’m going to explain how it works with CoreData, then I’ll ask if or how it can be accomplished with Realm.</p>
  <p>TL;DR: I’m hoping Realm has functionality similar to what you get with <code>NSFetchedResultsController</code>, specifically how it effortlessly manages table view sections.  If you know the answer you can probably skip the details below.</p>
  <p>Here’s the scenario. Let’s say we have a data model like this:</p>
  <pre><code>￼┌────────────────┐
  │Category        │
  │ - name         │             ┌────────────────┐
  │ - description  │┼─────┐      │Item            │
  │ - items        │      │     ┌│ - name         │
  │ - dateCreated  │      └────○┼│ - description  │
  └────────────────┘            └│ - category     │
                                 │ - dateCreated  │
                                 └────────────────┘
                                 
  </code></pre>
  <p>A <code>Category</code> can have many <code>Items</code>. An <code>Item</code> has one <code>Category</code>.</p>
  <p>And let’s say we’re displaying some <code>Item</code>s in a table view. The table view is sectioned by <code>Category</code>. So it looks like this:</p>
  <pre><code>￼┌──────────────┐
  │Camping       │
  │ Axe          │
  │ Firestartor  │
  │Clothing      │
  │ Bug shirt    │
  │ Long pants   │
  │              │
  └──────────────┘
  </code></pre>
  <p>Yes, there’s a typo. We’ll get to that. Camping is a <code>Category</code> and is displayed in a section header. Its <code>Item</code>s are the rows in that section. Same with Clothing, it’s a <code>Category</code> and it’s in the section header and its <code>Item</code>s are the rows in that section.</p>
  <p>With CoreData this is easy to accomplish with an <code>NSFetchedResultsController</code> like this:</p>
  <pre><code>let fr = NSFetchRequest&lt;Item&gt; = NSFetchRequest(…)
  fr.predicate = …
  fr.sortDescriptors = …
  let frc = NSFetchedResultsController(fetchRequest: fr, managedObjectContext: moc, sectionNameKeyPath: “category.name”, cacheName: nil)
  </code></pre>
  <p>The fetch request, <code>fr</code>, is a lot like a Realm query. It specifies the entity along with a predicate and sort descriptors. In this case assume the sort descriptors are saying to sort by the item name within the category name.</p>
  <p>But for sections, the key is providing a value for the <code>sectionNameKeyPath</code> parameter. The value is relative to the entity you specify in the fetch request. So in this case <code>category.name</code> is relative to <code>Item</code>. <code>Item</code> has a <code>Category</code> and a <code>Category</code> has a name. So the sections are going to be <code>Category</code> names.</p>
  <p>Now, in <code>cellForRowAt</code> I just do this</p>
  <pre><code>let item = frc.object(at: indexPath)
  cell.textField.text = item.name
  </code></pre>
  <p>Since we provided the <code>sectionNameKeyPath</code>, the fetched results controller has already done all the work and has sorted out the sections and knows which <code>Item</code>s are in what sections. That’s all there is to loading a sectioned table view with <code>NSFetchedResultsController</code>.</p>
  <p>Furthermore, like a Realm collection observer, <code>NSFetchedResultsController</code> listens for changes. In this case it’s listening for changes to <code>Item</code>s. Let’s say the user taps another tab on the tab bar and goes someplace else in the app, notices that typo for &quot;firestartor&quot; and fixes it. The <code>frc</code> back on the other view controller will notice that change and call its delegate method, where it hands me the indexPath for that row, and I can just update the cell. I don’t need to do any kind of processing to figure out the index path, it simply hands me the correct index path.</p>
  <p>Questions:</p>
  <ol>
  <li>Does Realm have a mechanism like <code>NSFetchedResultsController</code> that makes working with sections easy or is the section lovin’ entirely up to the developer?</li>
  </ol>
  <p>The docs for RealmCollectionChange say...</p>
  <blockquote>
  <p>The change information is available in two formats: a simple array of row indices in the collection for each type of change, and an array of index paths in a requested section suitable for passing directly to UITableView’s batch update methods.</p>
  </blockquote>
  <ol start="2">
  <li>All I'm getting is the simple array of row indices. How do you get the other format? The one with the array of index paths?</li>
  </ol>