我有以下三种型号:
Show.h
@class Season;
@interface Show : NSManagedObject
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSSet *seasons;
@end
Season.h
@class Episode, Show;
@interface Season : NSManagedObject
@property (nonatomic, retain) NSNumber * season;
@property (nonatomic, retain) NSSet *episodes;
@property (nonatomic, retain) Show *Show;
@end
Episode.h
@class Season;
@interface Episode : NSManagedObject
@property (nonatomic, retain) NSNumber * watched;
@property (nonatomic, retain) Season *season;
@end
关系是:Show有很多季节,而Season有很多剧集,Episode有一个值得观看的bool值。现在我想得到下一集,它的观看价值从它的节目中没有。如何编写这个谓词?
答案 0 :(得分:0)
您的剧集无序,将number
属性添加到Episode
实体。
更新了答案。基本上你添加排序描述符,以便季节的season
属性返回值,我假设它是它的数字和一个名为Eposode
的新number
属性
- (Episode *)nextUnwatchedEpisodeForShow:(Show *)show
{
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([Episode class])];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"season.Show == %@ && watched == %@", show, @NO];
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"season.season" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:@"number" ascending:YES]];
return [[self.managedObjectContext executeFetchRequest:fetchRequest error:nil] firstObject]; // first object should be the first unwatched episode, if it's nil then all the episodes were watched
}