views:

1487

answers:

1

Hi,

I'm new to developing for the iPhone and I'm using Core Data for my data management. My managed objects have a property called creationDate and I need to have a list of all distinct dates that are in the database. To reduce the overhead I set

[fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:@"creationDate"]];

because that's all that's needed.

I know I can set [fetchRequest setReturnsDistinctResults:YES]; to get the distinct dates, but how can I take only the date part of an NSDate in a fetch request?

thanks,

Thomas

P.S. In SQL this would look something like: SELECT DISTINCT DATE(creationDate) FROM events;

+2  A: 

By "date part of an NSDate" I assume you mean as distinct from the time? You can't directly express that for the purposes of distinct results if the entry in the store has the time encoded. What you can do is split the creationDate in your model to a two properties, date and time, and the normalize all the dates to a single time (or if you don't care about the time you can just throw out the time). If you actually still need the time it is there, but you can do queries purely against the date.

If your entity has its own subclass you can do that by writing a custom setter that takes the data, extracts the components via NSDateComponents, and reassembles those into a date via NSCalendar.

Yes, it seems like it should be simpler to deal with dates, but it is necessary to take a somewhat roundabout path.

Louis Gerbarg