Some guidance would have been nice, as I am a bit out of the depths of the problem with Core Data. I would like to create an NSFetchRequest with an NSSortDescriptor that uses a dependent property based on relationships with many.
For part of Apple, the documentation states that you cannot do this. What confuses me is that only when I use it as part of an NSSortDescriptor does it not work. In the request NSPredicate and in my subclass of ManagedObject it works.
In short, I have a simple object model that includes a Venue object that can have many Checkin objects. Each Checkin object has a property called hereNow.
@interface Venue : NSManagedObject <MKAnnotation> {}
@property (nonatomic, readonly, retain) NSNumber * hereNow;
@end
@interface Venue (CoreDataGeneratedAccessors)
- (void)addCheckinsObject:(NSManagedObject *)value;
- (void)removeCheckinsObject:(NSManagedObject *)value;
- (void)addCheckins:(NSSet *)value;
- (void)removeCheckins:(NSSet *)value;
@end
@implementation Venue
- (NSNumber *)hereNow {
return [self valueForKeyPath:@"checkins.@sum.hereNow"];
}
@interface Checkin : Update {}
@property (nonatomic, retain) Venue * venue;
@property (nonatomic, retain) NSNumber * hereNow;
@end
@implementation Checkin
@dynamic venue;
@dynamic hereNow;
@end
So far so good. Here's the fetchRequest from the TableView controller
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Venue" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"checkins.@sum.hereNow > 0"];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"checkins.@sum.hereNow" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:nil
cacheName:@"Venues"];
- "NSInvalidArgumentException", : " , KVC, ; . @sum.hereNow '
fetchRequest MapView, ( NSSortDecriptor ). TableView fetchRequest, , , . , , , sortDecriptor.
Apple, , ;
"-". , , Order to-many (orderItems) OrderItem, OrderItem . , Order totalPrice, OrderItem . , keyPathsForValuesAffectingValueForKey: orderItems.price totalPrice. OrderItem orderItems , totalPrice.
, , ? Core, . KVO, KVC .., , . , , , , "hereNow". , ? , !