views:

46

answers:

1

I have some XML data retrieved from a web service that I use to create NSManagedObjects 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 NSManagedObjects 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?

+1  A: 

Is there a way to see the actual SQL statements generated by CoreData so I can see truly what's going on?

Yes. In your Active Executable settings in Xcode, add -com.apple.CoreData.SQLDebug 1 to the "Arguments to be passed on launch" list and check the checkmark. Core Data will then NSLog its SQL queries.

Ole Begemann
Thanks! I actually figured out the problem was that I wasn't loading the right data in the array against which I was running my queries. I was going to delete the question, but this is a good answer.
Ben Collins