predicate with NSDates

  • Hello,

    My Employee entity has a too many relationship called "myVacations"  to a
    Vacation entity which has date attributes "startDate" and "endDate". I want
    to find out which employees are on vacation on a specific NSDate. I am
    having trouble with the predicate below. What's the correct predicate
    format?

      NSDate *today = [NSDate date];
      NSEntityDescription *entityDescription = [NSEntityDescription
    entityForName:  @"Employee"  inManagedObjectContext: managedObjectContext];
      NSFetchRequest *request = [[NSFetchRequest alloc] init];

      [request setEntity: entityDescription];

      NSPredicate *predicate = [NSPredicate predicateWithFormat:  @"(
    myVacations.startDate < %@) AND ( myVacations.endDate > %@) ",  today,
    today];
      [request setPredicate: predicate];

      NSError *error =  nil ;
      NSArray *employeesOnVacationToday = [managedObjectContext
    executeFetchRequest:
    request error: &error];

    I get the following error:
      -[NSCFArray compare:]: selector not recognized [self = 0x3d3690]

    Thanks!
  • My Employee entity has a too many relationship called "myVacations"  to a

    Vacation entity which has date attributes "startDate" and "endDate". I want

    to find out which employees are on vacation on a specific NSDate. I am

    having trouble with the predicate below. What's the correct predicate

    format?

    NSDate *today = [NSDate date];

    NSEntityDescription *entityDescription = [NSEntityDescription

    entityForName:  @"Employee"  inManagedObjectContext: managedObjectContext];

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

    [request setEntity: entityDescription];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:  @"(

    myVacations.startDate < %@) AND ( myVacations.endDate > %@) ",  today,

    today];

    [request setPredicate: predicate];

    NSError *error =  nil ;

    NSArray *employeesOnVacationToday = [managedObjectContext

    executeFetchRequest:

    request error: &error];

    I get the following error:

    -[NSCFArray compare:]: selector not recognized [self = 0x3d3690]

    After reading here,

    http://developer.apple.com/documentation/Cocoa/Conceptual/Predicates/Articl
    es/pUsing.html


    I learned I should use the ANY operator when referring to a to-many
    relationship in a predicate format so I now have the following predicate and
    I no longer get the "selector not recognized" run time error. I also
    realized what I wanted is employees who are NOT on vacation on a specific
    day so I changed the logic to reflect that. However, when I use the
    predicate below in a fetch request I get an empty array. Some employees
    don't have any vacation days set so I added the check to match myVacations
    == nil but I still get an array with count of 0.
    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"((ANY
    myVacations == nil) OR ((ANY myVacations.startDate > %@) OR (ANY
    myVacations.endDate < %@)))", day, day];

    Does this predicate format describe the Employees I'm looking for? I would
    appreciate any help. Thanks.

    Andres
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today