NSPredicate to to-many relationship

时间:2013-10-09 13:57:34

标签: ios objective-c core-data nspredicate

我有对象Movie,它与多个关系events每个事件都有属性date 我需要创建带有具体事件的电影的fetchedResultController

- (NSFetchedResultsController *)fetchedResultsControllerForEntertainments:(Class)className date:(NSDate *)date
{
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass(className)];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
dateFormatter.dateFormat = @"yyyy/MM/dd";
NSDate *startDate = [dateFormatter dateFromString:[dateFormatter stringFromDate:date]];
NSDateComponents *oneDay = [NSDateComponents new];
oneDay.day = 1;
NSDate *endDate = [[NSCalendar currentCalendar] dateByAddingComponents:oneDay toDate:startDate options:0];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(events, $s, ($s.date >= %@ AND $s.date < %@)).@count > 0", startDate, endDate];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"objectId" ascending:YES];
fetchRequest.sortDescriptors = @[sortDescriptor];
fetchRequest.fetchBatchSize = 20;
NSFetchedResultsController *fetchResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
return fetchResultsController;
}

我想创建谓词,它会返回我的电影,其中包含当前日期的事件

1 个答案:

答案 0 :(得分:-1)

我认为这是一个错字 - $s.date >= %@ AND $s.date < %@就是这个 正确?那是不可能的情况,请检查一下。 编辑: 我错了。这是正确的