NSFetchRequest group by month

不问归期 提交于 2019-12-12 01:54:44

问题


In my model, I have a Sale entity, each Sale entity has a date property (NSDate). I want to compute the sum of sales for each month.

I know how to create the expression for computing the sum, what I want to find out is how do I tell NSFetchRequest to group by month.


回答1:


You can use a NSFetchedResultsController to do the grouping for you. It has a parameter in the factory method called sectionNameKeyPath which you can use to separate your data. You will get all the NSIndexPath arithmetic for free, and a myriad of optimizations as well.

Because dates are practically continuous, you have to work with transient properties, or even persist the month data (which is cheap, so it does not matter). Take a look at Apple's sample code with dates in section headers and proceed analogously.

Apple Sample Code: DateSectionTitles
Pay particular attention to the implementation of APLEvent.



来源:https://stackoverflow.com/questions/27461460/nsfetchrequest-group-by-month

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!