Re: Core Data entity fetching by date
Re: Core Data entity fetching by date
- Subject: Re: Core Data entity fetching by date
- From: Quincey Morris <email@hidden>
- Date: Sat, 8 Nov 2008 23:50:45 -0800
On Nov 8, 2008, at 18:25, Michael Swan wrote:
Each entity has a date, amount, & type (which is a to one
relationship to a type entity, the reverse is to-many). In the end I
want to break things down so that the app can tell you "Jan type A =
$300, type B = $100, ..."
I have a method that currently tells me how many entities are from
each month of the year and can easily then add the amounts. The
problem is that I have 8 pages of code so far and I haven't actually
added up the totals or split them be type. I am looking to see if
anyone has any ideas to make this be less code and therefore more
efficient. Below are the tedious steps I have to go through:
The issue is, I think, how many detail objects you have. ("Entities"
is not the correct word -- a Core Data entity is like a class, not
like an object.)
If there aren't huge quantities of them (say, less than a few
thousand), I'd suggest you just fetch them all and iterate through the
resulting array accumulating various totals. If necessary, you can
fault out each one after you've done with it, but there may not be
enough data associated with each for even that to be necessary.
If there are a lot of them, but they're well-distributed across types,
then I'd suggest you fetch all the types, and iterate through them.
For each type, iterate over all the objects of that type using its to-
many relationship (no fetch is needed, since the relationship is just
a NSSet). Fault out each type when you're done with it (and make sure
garbage collection runs, or use local autorelease pools, to clean up
memory -- or return to the main event loop between each type's
processing).
If you have many thousands of detail objects *per type*, then, yes,
futzing with by-the-month fetches might be the best solution. In that
case, you may decide it's worth simplifying things by adding a
separate "month" attribute to every entity -- using up a few more KB
of disk space. Or, you might be able to construct 12 suitable fetch
predicates in the Core Data model itself, so that you don't have to
build them in code. I found the predicate editor to be a little bit
obscure, but I expect you can figure it out.
Of course, if you have many thousands of detail objects *per type per
month*, then you're going to have to go further in your memory
management in order to avoid exploding your application's memory
footprint. The "Core Data Performance" section of the Core Data
programming guide talks about various issues and approaches. The
"Efficiently Importing Data" section is also useful for tips and
ideas, even though you aren't literally importing anything, but many
of the concepts are related.
HTH
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden