views:

28

answers:

1

I have a core data application and I would like to get results from the db, based on certain parameters. For example if I want to grab only the events that occured in the last week, and the events that occured in the last month. Is it better to do a fetch for the whole entity and then work with that result array, to create arrays out of that for each situation, or is it better to use predicates and make multiple fetches?

+1  A: 

The answer depends on a lot of factors. I'd recommend perusing the documentation's description of the various store types. If you use the SQLite store type, for example, it's far more efficient to make proper use of date range predicates and fetch only those in the given range.

Conversely, say you use a non-standard attribute like searching for a substring in an encrypted string - you'll have to pull everything in, decrypt the strings, do your search, and note the matches.

On the far end of the spectrum, you have the binary store type, which means the whole thing will always be pulled into memory regardless of what kind of fetches you might do.

You'll need to describe your managed object model and the types of fetches you plan to do in order to get a more specific answer.

Joshua Nozzi
I'm using the SQLite store type. What i'm trying to do is generate stats based on a set time interval, so get the average value of an item in a week, 30 days, and a year. So I was trying to figure out if its better to create 3 different fetches with different predicates or to instead just grab the entity and then enumerate through the returned array set, pushing the matching date values onto different arrays for calculation?
adam0101
Describe your model a bit? How are the stats/dates represented in your M.O.M.? Also, have you seen set and array operators? http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/ArrayOperators.html
Joshua Nozzi