0

I have a list of Task entities in my core data DB.

I want to query only the tasks that their dueDate property is Today.

Now, for technical reasons I want to save the query in my CoreData table as well. (in a different entity). Meaning I have to somehow tell sqlite, to return the current date. I tried using an NSPredicate as such to no avail:

dueDuate > date('now','start of day')

I got an exception:

Unable to parse function name 'date' into supported selector (date) 

Anyway to achieve what I try to do ?

Idan
  • 5,717
  • 10
  • 47
  • 84
  • http://stackoverflow.com/questions/25655066/coredata-nspredicate-date-function – Dheeraj Singh Dec 24 '14 at 13:18
  • well, now() seems to work in iOS8, but I need a way to get today at midnight. Know of any way ? – Idan Dec 24 '14 at 14:28
  • http://stackoverflow.com/questions/9040319/how-can-i-get-an-nsdate-object-for-today-at-midnight – Fantini Dec 24 '14 at 14:33
  • @Fantini I need it as part of a CoreData query that works with the now() method – Idan Dec 24 '14 at 15:18
  • Why do you need to use SQLite's `now` function? Using `NSDate` as described in the above links will get you what you seem to want. – Tom Harrington Dec 24 '14 at 22:24
  • @TomHarrington as I wrote in my question, I save the predicate as a sting in my db as well. Meaning I need one to persist and remain valid every time I use it (no matter which day it is), without knowing what's inside of it. Using [NSdate date] and other date methods is the easy solution, i weren't been asking if it were that easy. – Idan Dec 25 '14 at 08:52
  • You should explain what you actually want to accomplish with "saving the predicate to remain valid every time". It seems you are fighting against windmills. – Mundi Dec 25 '14 at 14:26
  • @Mundi I think it's pretty clear, I want to use a date method inside a query, but without specifying a hard coded date. the now() method works, but I need a way to manipulate it a bit more. – Idan Dec 27 '14 at 22:54
  • That's not what I mean. I mean what business logic problem are you trying to solve. The technical requirements seem to me to be quite arbitrary. – Mundi Dec 29 '14 at 15:41
  • I have a List that should contain all of today's tasks. I refresh that list every day to update it with the correct list it should contain for a certain day. I want to have the query saved in my db as well, so I can just run it without more date manipulations. (the reason is I have many lists as such, not just today). You have a better suggestion for me ? – Idan Dec 30 '14 at 11:36

1 Answers1

-1

It is not necessary nor feasible to store a query inside a Core Data entity which itself has to be queried and retrieved. It's like writing a telephone number for enquiring about an address on the door of this address.

The cleanest way to do this is to simply keep the date as an attribute and then construct simple predicates to retrieve your List objects for this date. All you need for this the beginning and the end of a certain day.

[NSPredicate predicateWithFormat:"dueDate > %@ && dueDate < %@", dayStart, dayEnd]

That's it. Your requirement "without more date manipulations" is not realistic, because you will need to calculate the start and end of day dynamically. It is nonsense to store this in Core Data's persistent store.

So to get the start of day of any date (written as a category on NSDate):

-(NSDate *)startOfDay { 
  NSDateComponents *components = [[NSCalendar currentCalendar] components:
    NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay 
    fromDate: self];
  components.hour = 0; components.minute = 0; components.second = 0;
  return [[NSCalendar currentCalendar] dateFromComponents:components];
}

And the end of any day

-(NSDate *)endOfDay {
   NSDateComponents *components = [[NSDateComponents alloc] init];
   components.day = 1;
   return [[NSCalendar currentCalendar] dateByAddingComponents:components
          toDate:self.startOfDay options:0];
}

For the predicate above you just substitute dayStart and dayEnd with [[NSDate date] startOfDay] and [[NSDate date] endOfDay] respectively.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • I never said I will store the predicate in the same entity as the one I'm fetching. What I meant was storing the predicate (which queries TASK entities) inside a property on the LIST entity. Again, how to get the today and tmw's midnight was never my question. And of course it's realistic, otherwise it wouldn't be possible in sqlite to do date operations, which of course it is. I'm just asking how to do it core data. Thanks though. – Idan Jan 01 '15 at 09:36
  • Again, it does not make any sense to the store predicates in Core Data. You should take the advice of experienced programmers who take the time to look at your problem. – Mundi Jan 01 '15 at 09:40
  • I'm an experienced programmer myself, I usually don't take the advice of people just because they tell me to take their advice, unless it comes with logical explanation. Unfortunately you provided none. I do appreciate your time. – Idan Jan 03 '15 at 11:20
  • I did provide an explanation. Your copious experience might be still insufficient to appreciate it. I think I have made the main point - the futility of storing query information in the data to be queried - quite clear. Your down-vote is not justified in view of the quality of my answer. – Mundi Jan 03 '15 at 19:18