需要按顺序排序通过获取请求检索的类别列表,以排序类别产品的数量/数量排序。然后,我希望能够手动添加几个类别(将其过滤掉)到数组中,以便它们位于列表的底部。当我尝试按产品数量进行排序时,我一直在遇到“包含KVC聚合的Keypath的错误”,该错误不应该有一个......“。基于对多对多关系中的相关对象计数对排序请求进行排序
我在这里错过了什么/做错了什么?谢谢!
Category.h
@class Category;
@interface Category : NSManagedObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSSet *products;
@end
Product.h
@class Category;
@interface Product : NSManagedObject
@property (nonatomic, strong) NSString *sku;
@property (nonatomic, strong) NSString *name; // Product name
@property (nonatomic, strong) NSString *prodDescription;
@property (nonatomic, strong) Category *category; // Product category
@property (nonatomic, strong) NSString *upc;
@property (nonatomic, strong) NSString *countryCode;
@property (nonatomic, strong) NSString *webpage;
@property (nonatomic, strong) NSString *manual;
@property (nonatomic, strong) NSString *quickStart;
@property (nonatomic, strong) UIImage *thumb;
@property (nonatomic, strong) NSString *thumbURLString;
@property (nonatomic, strong) UIImage *mainImg;
@property (nonatomic, strong) NSString *mainImgURLString;
@property (nonatomic, strong) UIImage *secondaryImg;
@property (nonatomic, strong) NSString *secondaryImgURLString;
@end
CategoriesViewController.m
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController == nil) {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Category" inManagedObjectContext:self.managedObjectContext]]; // Fetch categories
NSArray *sortDescriptors = nil;
NSString *sectionNameKeyPath = nil;
// Sort by number of products in category
sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"[email protected]" ascending:NO]];
[fetchRequest setSortDescriptors:sortDescriptors];
// Use predicate to exclude Accessories and Replacement Parts categories from initial array
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name != 'Accessories' AND name != 'Replacement Parts'"];
[fetchRequest setPredicate:predicate];
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:sectionNameKeyPath
cacheName:nil];
}
return _fetchedResultsController;
}