1

我无法为 fetchedResultedController 创建 fetchRequest。

我有 3 个模型:

@interface Event : NSManagedObject
    @property (nonatomic, retain) Entertainment *entertainment;
    @property (nonatomic, retain) Place *place;
    @property (nonatomic, retain) NSDate * date;
    @property (nonatomic, retain) NSString * objectId;
    //boolValue
    @property (nonatomic, retain) NSNumber * threeDimensional;
    //others properties
    .......
@end


@interface Entertainment : NSManagedObject
    @property (nonatomic, retain) NSString * name;
    @property (nonatomic, retain) NSString * objectId;
    @property (nonatomic, retain) NSSet *events;
    @property (nonatomic, retain) NSString *classNaming;
    //others properties
    .......
@end


@interface Place : NSManagedObject
    @property (nonatomic, retain) NSString * name;
    @property (nonatomic, retain) NSString * objectId;
    @property (nonatomic, retain) NSSet *events;
    //others properties
    .......
@end

所以我需要在tableView(每行1个地方)显示具体日期(例如11月1日)和具体entertainment的所有事件时间(示例事件可能有时间14:30和第二个19:10,所以我需要显示所有这些具体地方的牢房时间)

例子:

在此处输入图像描述

所以并不难

- (NSFetchedResultsController *)fetchedResultsControllerForEntertainmentsWithEventsFromEntertainment:(Entertainment *)entertainment 
date:(NSDate *)date
{
    if (!place && !date)
        return nil;
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([Event class])];
    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:@"date >= %@ AND date < %@ AND entertainment.classNaming == %@ AND entertainment.objectId == %@", startDate, endDate, entertainment.classNaming, entertainment.objectId];
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"entertainment.name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
    fetchRequest.sortDescriptors = @[sortDescriptor];
    fetchRequest.fetchBatchSize = 20;
    NSFetchedResultsController *fetchResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"place.name" cacheName:nil];
    return fetchResultsController;
}

所以我得到了fetchedResultsController,其中每个部分都是带有行数组的地方,每个部分都是这个地方的事件。

但我有一个大问题:每个都Event可以 3D 显示(属性threeDimensional)。因此,如果 Place 有类似的 3D 和非 3D 事件,则必须将其分开,因此一个单元格将显示位置的 3D 事件和另一个非 3D 事件的位置。我做到了,但没有 fetchedResultsCONtroller。我需要在 fetchRequest 中使用什么谓词才能像这样:

在此处输入图像描述

4

0 回答 0