Sort items inside sections with NSFetchedResultsController and SortDescriptors
I am facing a problem with my custom UITableView when sorting rows. Let me explain in to you.
I have an UITableView with sections that use the NSFetchedResultsController to make a request and grab my objects from my database and separate them into sections based on a sectionNameKeyPath as follows:
var aFetchedResultsController = NSFetchedResultsController(fetchRequest: req, managedObjectContext: managedObjectContext!, sectionNameKeyPath: "dateStatus", cacheName: nil)
This works perfectly fine, it retrieves my data and populate two different sections like so: "Actually" : [1,2,3,4,5] "Soon" : [1,2]
My dateStatus
property is just a String type based on a double like so:
var dateStatus : String { get { return licenseStart.doubleValue / 1000 < NSDate().timeIntervalSince1970 ? DateStatus.Actually.rawValue : DateStatus.ComingSoon.rawValue } }
Until this part everything works just fine. But now I want to add a sort option that let the user sort the results based on the licenseStart
but keeping the sections in the same order. So I want to have my "actually" section first and the my "soon" section but inside both sections I want to sort the items ASC or DESC depending on the user choice.
So, right now if the user clicks on "order descendent" what I do if made a new NSSortDescriptors
array with:
[NSSortDescriptor(key: "licenseStart", ascending: false), NSSortDescriptor(key: "plan.planId", ascending: false)]
and a new afetchedResultsController!.performFetch()
with this new sort descriptor, but what I have after this new fetch is
"Soon" : [2,1] "Actually" : [5,4,3,2,1]
And this is not exactly what I want, the items inside the sections are sort in the right way but I want to keep the sections in the same order as before, ie:
"Actually" : [5,4,3,2,1] "Soon" : [2,1]
I've tried different approaches to achieve this behaviour with no luck, I've also tried to find a similar problem here on stackoverflow and google without any luck, so I am wondering is someone has faced the same problem.
Please feel free to ask if anything is unclear.
Thanks you so much
****** EDIT *******
I've read this in another Stackoverflow question but this does not apply to my case.. I'm wondering why.. (in my case the NSSortDescriptors use the first sort descriptor also to sort the elements within the sections)
A fetched results controller (FRC) uses only the first sort descriptor to group (and sort) the objects into sections. A second sort descriptor can be added to sort the objects within each section.
****** EDIT 2 *******
I realice that one probably good solution is to create a NSExpressionDescription to calculate the dateStatus
in the performFetch. I mean.. I can create a new column that could have two integer values 0 or 1 based on this expression: licenseStart.doubleValue / 1000 < NSDate().timeIntervalSince1970 ? 0 : 1
licenseStart.doubleValue / 1000 < NSDate().timeIntervalSince1970 ? 0 : 1
for the section name
then I am able to order first using this new column and then using other filters, so in both I'll order using this for sectionNameKeyPath with equal ascending and then using other filters.
But I don't know exactly how to do this using the NSExpressionDescription.
链接地址: http://www.djcxy.com/p/36162.html上一篇: iPhone SDK:核心数据,如何在NSFetchedResultsController中对部分进行排序?
下一篇: 使用NSFetchedResultsController和SortDescriptors对部分内的项目进行排序