Here is an example of how you would setup your query to find only weeks where the sum of the duration of the days is greater than 100.
NSManagedObjectContext *context = ...;
NSManagedObjectModel *model = ...;
NSFetchRequest *fr = [[NSFetchRequest alloc] init];
fr.entity = [model.entitiesByName objectForKey:@"Week"];
//This predicate will be compiled into pure SQL
fr.predicate = [NSPredicate predicateWithFormat:@"days.@sum.duration > 100"];
NSError *error = nil;
NSArray *results = [context executeFetchRequest:fr error:&error];
if (error) {
NSLog(@"ERROR: %@", error);
}
NSLog(@"Results: %@", results);
You can actually implement the computed property in a similiar way, just add this to the NSManagedObject subclass backing your Week entity:
- (NSNumber *) duration {
return [self valueForKeyPath:@"days.@sum.duration"];
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…