Can I use more than one sectionNameKeyPath in fetchedResultsController?

I have a list of "accounts" in the master data. For each account, I want a separate section in the UITableViewController. I want these invoices to be sorted by quantity.

I am using fetchedResultsController for this. Each invoice has an amount and billID. I did sortDescriptor 'amount' and sectionNameKeyPath 'billID'. This causes problems because the didChangeSection is not called appropriately when needed.

I did a little tinkering and came across this post: Custom Crash Section Name NSFetchedResultsController

And it looks like Apple's documentation says that the main sort descriptor should be the same as in theNameKeyPath section.

I kept sortDescriptor as "sum" and changed sectionNameKeyPath as "billID". This fixed the behavior. However, if two accounts have the same number, they will be in the same section, which I don't want. Anyway, I can have a sectionNameKeyPath depending on two variables like "amount" and "billID", so I can guarantee a separate section for each invoice, but sort it by number?

Thanks in advance.


source to share

1 answer

The property sortDescriptor


is actually an array. We can pass more than one sortDescriptor

to it based on the property order so that the results are sorted.

Try this order sortDecriptor


request.sortDescriptors= @[billIdSortDescriptor, amountSortDescriptor];


Here request

is this NSFetchRequest

passed in. NSFetchedResultsController

Thus, the results will first be sorted by account id, and objects with the same billID

will be sorted by amount.



All Articles