两个日期之间的NSPredicate返回0结果

时间:2015-01-05 13:40:30

标签: ios objective-c nsdate nspredicate nsfetchrequest

我正在尝试根据“开始”(NSDate)字段检索一些结果。但是,它总是返回0结果。 我无法弄清楚我做错了什么。我猜它与NSDate类型有关,但我已经仔细检查过,一切似乎都正确。

我已经检查了this个答案,还有this个答案。但无济于事......

非常感谢任何帮助。

以下是模型文件中的定义:

@property (nonatomic, retain) NSDate * begin;

这是我调用获取请求的代码。

-(void)getCalendarListForDatesFrom:(NSDate  *)startDate 
                                To:(NSDate  *)endDate 
                           inViews:(NSArray *)daysArray
{

    // Reset dates to Midnight
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    NSUInteger preservedComponents = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit);

    startDate = [calendar dateFromComponents:[calendar components:preservedComponents    
                                                         fromDate:startDate   ]
                ];
    endDate =   [calendar dateFromComponents:[calendar components:preservedComponents 
                                                         fromDate:endDate     ]
                ];


    NSLog(@"Get Events for dates %@ to %@", startDate, endDate);

    _isRetrievingData = YES;

    NSManagedObjectContext *MOC;
    NSPredicate *predicate;
    NSError *error              = nil;
    NSString *entityName        = @"CalendarEvent";

    //AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    //BOOL doIHaveConnexion = [appDelegate getConnexion];

    MOC = self.managedObjectContext;
    NSEntityDescription *entity = [NSEntityDescription entityForName:entityName 
                                              inManagedObjectContext:MOC        ];

    // Fetch Request
    NSFetchRequest *fetchRequest;
    fetchRequest = [[NSFetchRequest alloc] init];
    if (!fetchRequest) 
    {
        fetchRequest = [[NSFetchRequest alloc] init];
    }

    [fetchRequest setEntity:entity];

    // Filter
    NSLog(@"Filter results between dates %@ and %@", startDate, endDate);
    predicate = [NSPredicate predicateWithFormat:@"(%K >= %@) AND (%K <= %@)", @"begin", startDate, @"begin", endDate];
    //predicate = [NSPredicate predicateWithFormat:@"(%K > %@)", @"begin", [NSDate date]];

    NSLog(@"PREDICATE = %@", predicate);
    NSLog(@"Set Predicate");

    [fetchRequest setPredicate:predicate];

    NSLog(@"Set Sort Descriptor");
    NSSortDescriptor *beginDateDescriptor = [[NSSortDescriptor alloc] initWithKey:@"begin" 
                                                                        ascending:YES 
                                                                         selector:@selector(localizedCaseInsensitiveCompare:)
                                            ];
     NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:beginDateDescriptor, nil];

     NSLog(@"Fetch");
     [fetchRequest setSortDescriptors:sortDescriptors];


     NSLog(@"Init Fetch");
     // Initialize Fetched Results Controller
     if (!self.fetchedResultsController) 
     {
         self.fetchedResultsController = [[NSFetchedResultsController alloc]  
                                                 initWithFetchRequest:fetchRequest 
                                                 managedObjectContext:MOC 
                                                   sectionNameKeyPath:nil 
                                                            cacheName:nil          ];
     }

     NSLog(@"Perform Fetch");
     // Perform Fetch
     [self.fetchedResultsController performFetch:&error];

     NSArray *calEvents = [MOC executeFetchRequest:fetchRequest error:&error];

     currentCalEvents = [calEvents mutableCopy];

     if (error) 
     {
         NSLog(@"Unable to execute fetch request.");
         NSLog(@"%@, %@", error, error.localizedDescription);
     } 
     else 
     {
        NSLog(@"TOTAL CALENDAR EVENTS %lu",(unsigned long)[calEvents count]);
        NSLog(@"currentCalEvents = %@", currentCalEvents);

        [self addCalendarEventsToDays: daysArray];
     }

     fetchRequest = nil;
}

以下是来自控制台的日志:

Filter results between dates 2015-01-02 23:00:00 +0000 and 2015-01-07 23:00:00 +0000
PREDICATE = begin >= CAST(441932400.000000, "NSDate") AND begin <= CAST(442364400.000000, "NSDate")
Set Predicate
Set Sort Descriptor
Fetch
Init Fetch
Perform Fetch
TOTAL CALENDAR EVENTS 0

0 个答案:

没有答案