Sorting a Table View of Core Data Records

October 8th, 2014

Filed under: Cocoa, Mac Development | 1 comment

In my previous post, I detailed the steps needed to bind a view-based table view to a Core Data entity. That post is missing one thing: sorting, which I’m going to cover now.

Add an Ordering Attribute to the Entity

Core Data does not have built-in support for sorting records. If you want a collection of entities sorted, you must add an attribute to the entity that stores its position in the collection.

Write Code to Create Sort Descriptors

To sort the table view you first need to bind the array controller’s Sort Descriptors binding. But the array controller needs something to bind to. You must write code to create an array of sort descriptors, NSSortDescriptor, which you will later bind to the array controller. Supply the name of your entity’s ordering attribute as the sort descriptor’s key. The following code shows how to create the sort descriptors in Swift:

func chaptersSortDescriptors() -> [NSSortDescriptor!] {
    return [NSSortDescriptor(key: "order", ascending: true)]
}

My entity attribute is named order. Substitute the name of your ordering attribute in your code. You can also use a different method name than chaptersSortDescriptors.

I’m writing a document-based application so I put my sort descriptors method in my NSPersistentDocument subclass. I have not tried this with a shoebox (non-document-based) application. My best guess is you would place the sort descriptors method in your AppDelegate file.

Bind the Array Controller’s Sort Descriptors

Now it’s time to bind the array controller’s Sort Descriptors binding to the method you wrote that creates the array of sort descriptors.

  1. Select the array controller from the object list in Interface Builder.
  2. Open the bindings inspector.
  3. Click the disclosure triangle next to the Sort Descriptors binding.
  4. Select the Bind to checkbox.
  5. Choose File’s Owner from the menu.
  6. In the Model Key Path text field, enter the name of the method you wrote that creates the sort descriptors.

CoreDataArrayControllerSortDescriptorsBinding

For Step 5, I’m not sure if you bind to File’s Owner for a shoebox application. You many need to bind to the application instead of File’s Owner.

Bind the Table View’s Sort Descriptors

The final step is to bind the table view’s Sort Descriptors binding to the array controller’s sort descriptors.

  1. Select the table view from the object list in Interface Builder.
  2. Open the bindings inspector.
  3. Click the disclosure triangle next to the Sort Descriptors binding.
  4. Select the Bind to checkbox.
  5. Choose the array controller from the menu.
  6. In the Controller Key text field, enter sortDescriptors.

CoreDataTableViewSortDescriptorsBinding

Tags:


One thought on “Sorting a Table View of Core Data Records

  1. David says:

    First. Thank you for your treatment of this topic. Your step-by-step explanations made a conceptually simple, yet implementation-wise amazingly convoluted, topic much less painful to work through. That said, your method of implementing a sort descriptor throws an exception, at least for me. The issue seems to be the bindings expect to see an array of NSSortDescriptor as opposed to a function that returns such an array. You’ll note even in the image you posted, there is a greyed ‘!’ next to the function call because Xcode can resolve the key path. Interestingly, the function is still somehow used when the column header is clicked, despite throwing an exception while doing so.

    To solve this problem, I created a simple var declaration in my App Delegate instead of your func.

    var sortDescriptors:[NSSortDescriptor] = [NSSortDescriptor(key: “entity-attribute-to-sort-on”, ascending: true)]

    This variable does appear as part of the autocompleting choices of Model Key Path if you bind your Array Controller to the App Delegate and seems to work as expected.

Leave a Reply

Your email address will not be published. Required fields are marked *