如何从具有CoreData的嵌套对象集中获取bool值为YES的对象

时间:2014-06-25 10:07:28

标签: ios objective-c core-data

我有以下三种型号:

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值。现在我想得到下一集,它的观看价值从它的节目中没有。如何编写这个谓词?

1 个答案:

答案 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
}
相关问题