NSFetchRequest sort by dependent property using to-many relationship

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

// Create and configure a fetch request with the Venue entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Venue" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

// Create a predicate to filter the results
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"checkins.@sum.hereNow > 0"];
[fetchRequest setPredicate:predicate];

// Create the sort descriptors array.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"checkins.@sum.hereNow" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];

// Create and initialize the fetch results controller.
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". , ? , !

+3
1

, :

  • SortDescriptor @sum . . , " KVC". "" , .

  • Venue, , . "hereNow" Checkin, , Venue. , .

sort, initWithKey: @ "hereNow".

, MapView ( ), , .

, , , , Checkins. , , , .

, keyPathsForValuesAffectingValueForKey Mac, iOS. , . . : https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueObserving/Articles/KVODependentKeys.html#//apple_ref/doc/uid/20002179-BAJEAIEE

CoreData, : https://pragprog.com/titles/mzcd/core-data

, .

+3

All Articles