假设我有一个 Podcast 实体,它有很多情节,我很困惑其中哪一个是剔除和排序的首选选项:
// Always work with the relationship property
- (NSSet*)unfinishedEpisodes {
NSArray* episodes = self.episodes.allObjects;
NSPredicate* predicate = [NSPredicate predicateWithBlock:^BOOL(PodcastEpisode* episode, NSDictionary* bindings) {
return !episode.isFinished;
}];
NSArray* unfinishedEpisodes = [episodes filteredArrayUsingPredicate:predicate];
return [NSSet setWithArray:unfinishedEpisodes];
}
- (NSArray*)unfinishedEpisodesSortedByAge {
NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES];
return [self.unfinishedEpisodes.allObjects sortedArrayUsingDescriptors:sortDescriptors];
}
或者
// Fetch specific sets of data as needed
- (NSArray*)unfinishedEpisodes:(NSArray*)sortDescriptors {
NSFetchRequest* fetch = [[NSFetchRequest alloc] initWithEntityName:@"Episode"];
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"podcast == %@ AND playcount == 0", self];
fetch.predicate = predicate;
fetch.sortDescriptors = sortDescriptors;
NSArray* results = [KRTDataManager.sharedManager.mainObjectContext executeFetchRequest:fetch error:nil];
return results;
}
- (NSArray*)unfinishedEpisodesSortedByAge {
NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES];
return [self unfinishedEpisodes:@[ sortDescriptor ]];
}
我在 SQL 中尽可能多地做的那部分很难认为选项 1 更好,但我读过的大部分内容似乎表明,一旦播客对象进入,使用 (NSSet*) 剧集非常便宜存在。我对在这些情况下如何处理核心数据故障的理解非常不稳定,我意识到核心数据不应该真正与 SQL 数据库进行比较。不过,仅基于这两个选项的构造方式,我认为将谓词和排序描述符直接烘焙到 fetch 中会有一些好处;但也许这不足以弥补关系所提供的收益。
谢谢。