views:

171

answers:

2

Hi,

I have an entity "Event" witch contains two properties: date (NSDate) and repeat (NSInteger - 0 = NONE, 1 = DAILY, 2 = WEEKLY, 3 = MONTHLY, ...).

Does anyone knows how can I filter events by repeats passing a date ?

Example:

First event: 01-01-2010 / weekly
Second event: 10-02-2010 / monthly
Current date: 10-06-2010

Request:
Get all events where Event.date == "Current date" OR Event.date.day == "Current date".day if Event.repeat == monthly

Returned event:
Second event

I hope someone understand what I'm trying to explain :s

A: 

I am not certain this can be done at the SQLite level. It certainly can be done once your events are pulled into memory but that, I suspect, defeats your goal.

There might be some clever way to de-normalize the data and thereby create a situation that can be filtered.

For example, if you had the day of month and month pulled out into integer fields you might be able to devise a way to determine based on those if things align. It is not coming to me directly but I would definitely look in that direction for a solution.

Another alternative, one that calendars tend to use, is to create dependent events for the specific dates coming up. When the event is created you create all of the dependent events so that you are just filtering on a date. Nasty to be sure.

Last option is to pull all events into memory and calculate from there.

Marcus S. Zarra
Thanks for your answer.I was searching for the best way to do that and asking me if it was possible with a "simple" nspredicate.
A: 

The problem here is that NSDate doesn't have a day (or any other similar) property. If it did, your predicate would work as written.

Data and time programming is deceptively complex under the hood. For example, in common usage, the phrase "same date" means the exact same calendar day. However, from the codes perspective it also means the same week, month and year because days are no more significant to code than any other arbitrary calendar division. Even in ordinary usage, "day" can refer to a specific range of hours e.g. Saturday, August 21 2010 or it can refer to any arbitrary range of 24 hours as in, "within a day." Which one do you need for this app?

NSDate is really an object wrapper around a microsecond accurate timestamp. It has methods for converting to strings and for creating and comparing timestamps but it doesn't understand calendar attributes such seconds, minutes, hours, days, weeks, month, or years. That is what NSCalendar is for and Core Data does not innately support that class as a data type as doing so for all possible calendars would be to complex.

If calendar attributes are required in a model, you need to create an custom entity that models a calendar date. Set the entities attributes to calendar attributes you need to model and then a relationship to the object that needs the calendar date as a property e.g.

CalendarDate{
    date:NSDate
    minute:int
    hour:int
    day:int
    month:int
    year:int
    events<--(required,nullify)-->>Event.date
}

You can create a custom class for the entity and provide a method that automatically populates the object based on the passed NSDate and any calendar you choose.

Now your predicate is easy.

NSPredicate *myPred;
myPred=[NSPredicate predicateWithFormat:@"(date.date==%@ or date.day==%i) AND repeat=%i", currentDate, 45, kMonthly]; 

This seems like it is cumbersome but it is required owing to the true complexity of calendar dates. There simply isn't an easy way to handle date and time calculations and comparisons for all uses.

TechZen
Thanks a lot for your answer, your solution seems to be really good.