iOS中相同属性的两个谓词

时间:2014-12-30 06:39:39

标签: ios objective-c nspredicate nsfetchedresultscontroller nsfetchrequest

我有一个bookingDate属性,我正在尝试根据bookingDate应用两种不同的提取条件。

    NSPredicate *confirmBookingPredicate = [NSPredicate predicateWithFormat:@"bookingDate > %@ AND ANY cab.confirmationStatus == 1 OR cab.confirmationStatus == 0", [NSDate date]];
    NSPredicate *cancelBookingPredicate = [NSPredicate predicateWithFormat:@"bookingDate < %@ AND bookingDate >= %@ AND ANY cab.confirmationStatus == 1", [NSDate date], [NSDate dateWithNoTime:[[NSDate date] dateByAddingTimeInterval:MINUSFORTYFIVEDAYS]]];

    NSPredicate *nilPredicate = [NSPredicate predicateWithFormat:@"cab.@count != 0"];
    NSPredicate *DatePredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[confirmBookingPredicate, cancelBookingPredicate, nilPredicate]];

    NSFetchedResultsController *fetchedResultsController;

    NSFetchRequest *fetchRequest = [Trip MR_requestAllSortedBy:@"bookingDate" ascending:YES];
    [fetchRequest setFetchBatchSize:10];
    [fetchRequest setPredicate:DatePredicate];
    [fetchRequest setReturnsObjectsAsFaults:NO];
    fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_defaultContext sectionNameKeyPath:@"bookingId" cacheName:nil];

但它没有按预期运作。我不明白为什么。请指导。

1 个答案:

答案 0 :(得分:0)

您说您希望将来的所有预订(已确认/已取消),并且仅取消今天之前的预订。请尝试使用谓词。

NSPredicate *futureBookings = [NSPredicate predicateWithFormat:@"bookingDate > %@", [NSDate date]];
NSPredicate *cancelledPreviousBookings = [NSPredicate predicateWithFormat:@"bookingDate < %@ AND cab.confirmationStatus == 1", [NSDate date]];
NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[futureBookings, cancelledPreviousBookings]];