如何分组从Core Data,iOS开发的抓取结果 [英] How to group fetched result from Core Data, iOS dev

查看:123
本文介绍了如何分组从Core Data,iOS开发的抓取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个核心数据,其实体名为费用,如:
实体:费用
属性:日期,类别,金额 .....

I have a core data with an Entity called "Expense", its like: Entity: "Expense" Attributes: "Date", "Category", "Amount"..........

我的问题是,是否可以按年份或年月份提取多少费用?
我知道如果抓取是基于时间段或类别,我可以使用NSPredicate过滤抓取的结果。但是我的头脑中的Date属性应该按年和月分隔,因此我可以将它们分组为SQL查询。是否可以直接在抓取?否则,唯一的方法是获取整个数据到一个NSArray和NSPredicate做一些过滤器。

My question is that is it possible to fetch how many expenses by year or year-month? I know if the fetch is based on time period or category, I can use NSPredicate to filter the fetched result. But the "Date" attribute in my mind should be seperated by year and month, so I can group them as in SQL query. Is it possible to do that directly in fetch? Otherwise the only way is to fetch the whole data to a NSArray and do some filters by NSPredicate.

我已经搜索了这几天,但没有找到直接的答案。现在我实际上有点相信没有直接的解决方案。但我仍然认为想检查如果任何人可以提供给我一些更好的解决方案coz每个开发人员必须解决这个问题。

I have searched this for days but find no direct answer. Now I actually kinda believe there is no direct solution. But I still think want to check out if anyone can provide me some better solution coz every developer must have solved this problem.

任何建议将非常感谢,谢谢你们:)

Any suggestion will be much appreciated, thank you guys :)

==========================新增==== =================
此外,我想基于Year-Month的feteched结果在UITableView中被索引列出,直到我知道有关索引列表的技术。我可能根据每个索引的年 - 月提供每个阵列的费用。是否有更好的方法?

============================Added======================= Moreover, I would like to the feteched result based by "Year-Month" to be index-listed in UITableView, as far as I know about the technique about indexed list. I'm likely to provide every array with Expenses according to every indexed "Year-Month". Is there a better way to do that?

我基于数据结构的想法是建立一对多的关系,从实体(年)到多个实体strong> year ,month,year-month)to-many Entity(year-month,Date,Amount ....)。也许这将给我一些方便,当我获取每个数组的索引UITableView。

My thinking based on data structure is to build some to-many relationship, from Entity(year) to-many Entity(year,month,year-month) to-many Entity(year-month, Date, Amount....). Maybe this will bring me some convenience when I fetch every array for indexed UITableView.

推荐答案

你可以实现@readonly属性和month在Expense类中,然后在谓词中使用它们。您不必存储这些值separatel,只需使用NSDateComponents在getter中计算它们。

You can implement @readonly properties "year" and "month" in Expense class and then use them in predicate. You don't have to store these values separatel, just compute them in getters using NSDateComponents.

这篇关于如何分组从Core Data,iOS开发的抓取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆