Has anyone got any tips for this problem i’m facing. Its not complex, but there might be a cleaner way to do it in Core Data.
I have a table of data (or managed object) with date and a value. I present this data normally showing each day and the value for each day.
I want to group this in weeks, so aggregate all rows that have a week of a certain number.
Example: my data is currently display like this
- Aug 5 | 5
- Aug 4 | 18
- Aug 3 | 8
- Aug 2 | 3
- Aug 1 | 10
- Jul 31 | 2
- Jul 30 | 1
- Jul 29 | 7
- Jul 28 | 6
etc etc
I want to display it like so:
- Week 24 | 47
- Week 23 | 13
- Week 22 | 20
etc etc
I’m already using an NSFetchedResultsController, and would like to continue to use this if possible. I think this is where the difficulty lies. If i was just presenting a UITableView and a NSArray of data, this would be easy to add up my values and present the data.
However, NSFetchedResultsController seems to be tied to fetching results (obviously), so I guess I want to aggregate this data using a fetch so that I can hook it right into my table view as I do with the raw data.
Any suggestions?
Thanks
I did this, but sort of an ugly way. But it works, and I needed to get it working quick…
Because I wanted to continue to use my FetchedResultsController, and all the architecture I’d created to hook it up to my view controllers, it seemed easier to have distinct entities for my Weeks and Months.
I know it goes against data storage principles by storing data that could be computed, but it was relatively easy to do.
I have my UsageDay managed object, which is the majority of the data. I then have a UsageWeek and UsageMonth object.
When I store a UsageDay object, from its date ivar, I calculate its weekOfYear and monthOfYear (and year) for both UsageWeek and UsageMonth.
UsageWeek contains: weekOfYear, year, and an NSSet of the UsageDay objects it contains
UsageMonth contains: monthOfYear, year, and an NSSet of the UsageDay objects it contains.
I can then easily get a list of all UsageWeek’s, which has a set of 7 UsageDay objects – on which i call [usageWeek valueForKeyPath:@”usageDaySet.@sum.value”] – which gives my the total. Works fine.
Feel free to openly criticize this design – I’m expecting it!!!