2015-12-14 63 views
1

需要按顺序排序通过获取请求检索的类别列表,以排序类别产品的数量/数量排序。然后,我希望能够手动添加几个类别(将其过滤掉)到数组中,以便它们位于列表的底部。当我尝试按产品数量进行排序时,我一直在遇到“包含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; 
} 

回答

0

你应该使用这样

一个一对多的关系

0

通常,这是通过在记录本身中有一个计数变量来解决的。这允许获取的结果控制器对该计数的值进行排序。我将这种计数维持在有关关系的定制二传手中。在你的情况下,这可能是products的集合,新变量可能被称为productCount。 Xcode有这种事情的片段模板。这里有一个例子:

- (void) addProductsObject: (Product *) value { 

    NSSet *changedObjects = [NSSet setWithObject:value]; 

    [self willChangeValueForKey: kDays withSetMutation: NSKeyValueUnionSetMutation usingObjects: changedObjects]; 
    [[self primitiveValueForKey: kDays] addObject: value]; 
    [self didChangeValueForKey: kDays withSetMutation: NSKeyValueUnionSetMutation usingObjects: changedObjects]; 

    self.productCount = (int32_t)self.products.count; 

} // -addProductsObject: 

其他三种方法同样简单。

相关问题