I have some XML data retrieved from a web service that I use to create NSManagedObject
s and store in a sqlite3 backing store. In my app, I'm using NSPredicate
objects to query this data by date (along with other fields). The data I have has records for every day from April 2009 through August 2010 (according to the raw XML I retrieved from the SOAP web service).
Here's a little setup:
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *today = [cal dateFromComponents:[cal components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit
fromDate:[NSDate date]]];
NSDate *day1ahead = [cal dateFromComponents:[cal components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit
fromDate:[NSDate dateWithTimeIntervalSinceNow:kSecondsPerDay]]];
NSDate *day2ahead = [cal dateFromComponents:[cal components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit
fromDate:[NSDate dateWithTimeIntervalSinceNOw:kSecondsPerDay*2]]];
I am able successfully retrieve NSManagedObject
s using an NSPredicate
query like this:
NSPredicate *todaysData = [NSPredicate predicateWithFormat:@"(StartDate >= %@) AND (StartDate <= %@)", today, day1ahead];
However, when I do a query like the next one, I get bupkis.
NSPredicate *tomorrowsData = [NSPredicate predicateWithFormat:@"(StartDate >= %@) AND (StartDate <= %@)", day1ahead, day2ahead];
I checked the XML data, and the future dates for which I'm trying to query are there, and I also checked the sqlite3 backing store directly like this:
sqlite> select datetime(startdate, "unixepoch", "31 years") from mydatatable;
The results of this simple query show that the future dates I'm after are there.
I even tried a query like this:
NSPredicate *allFuture = [NSPredicate predicateWithFormat:@"(StartDate >= %@)", today];
The results did not include anything beyond the current day. It seems to be in the backing store, so I'm a bit at a loss as to why the NSPredicate objects aren't working the way I expect them to.
Is there a way to see the actual SQL statements generated by CoreData so I can see truly what's going on? Am I looking at the data wrong in the sqlite3 db? Is there something else obvious that I'm missing?