Search code examples
objective-cnspredicatensfetchedresultscontrollernsfetchrequest

NSPredicate not filtering out dates accessed by dot notation though the dates don't fall in the query range


I have an expiration date of matches and I want objects with an expiration date greater than the current date to be filtered out of a fetch request...heres the code for the fetch request predicate:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:NSStringFromClass([SMLMatch class])inManagedObjectContext:[SMLCDManager mainQContext]];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"expirationDate.date > %@", [NSDate date]];
[fetchRequest setPredicate:predicate];
[fetchRequest setFetchBatchSize:20];
// Specify how the fetched objects should be sorted
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"updatedAt" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
DDLogDebug(@"Grabbing Chats/matches after date: %@", [NSDate date]);

Expiration date is a transformable object with a NSDate, "date"...

This query kind of works but seems to be returning weird results and I am wondering if the problem has more to do with the fact i am using dot notation to access the date variable on the SMLMatch match expiration...Any reason that accessing the date like this would be a problem?

Here's a log of output:

Grabbing Chats/matches after date: 2015-01-06 01:02:27 +0000

When I print out the expirationDate.date as they are displayed in the fetched result controller table view:

ExpirationDate: 2015-01-05 02:23:25 +0000 User: Zoe
ExpirationDate: 2015-01-05 09:35:22 +0000 User: Jamie

As you can see, Zoe and Jamie expiration date are indeed before the current date, and should not show up in the results, but amazingly they do show up in the query results! Also, looking at the time zones of all the printed out dates, they appear to all be at the +0000 time zone offset...Can I query off the current date using [NSDate date]? i have seen others construct date input off calendar, but that's only if you want to specify a different range or date that is not the current date.

Why would those dates be passing this predicate?

I construct the fetch results controller with this predicate in the following manner:

_fetchController = [[NSFetchedResultsController alloc] initWithFetchRequest:[self fetchRequest] managedObjectContext:[SMLCDManager mainQContext] sectionNameKeyPath:nil cacheName:nil];
_fetchController.delegate = self;

Solution

  • figured it out having a date be embedded in a transformable type is not query able using predicates...guess it seems obvious this would not work after the fact

    so, expirationDate.date, was the culprit. When i moved the date out to its own date property it worked.

    so the predicate simply changes to: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"expirationDate > %@", [NSDate date]];

    All's well that ends well