CoreData如何使用nsfetchedcontroller在UICollectionView中显示

时间:2016-07-13 10:36:37

标签: ios objective-c core-data

我有一个categoryData和一个具有多对多关系的itemData。 categoryData可以包含多个itemData,而itemData可以包含在一个或多个categorieData中。

我需要对项目所属的项目进行分组,并显示包含相关项目的类别列表。 categoryData是集合视图中的一个部分,itemData是该部分中的项目。

实施例

Category1 (section Header)
ItemA, Item B, ItemC (Items At indexPath)
Category2 
Item D, Item E, Item A

我正在使用以下代码设置nsfetchedcontroller来实现此目的。

+ (void)
getFetchedControllerForCategoryDiscoverDelegate:(id<NSFetchedResultsControllerDelegate>)delegate
                                     completion:(void (^)(NSFetchedResultsController *controller))
                                                    completion {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"showInDiscover == YES"];
      NSFetchedResultsController *controller = [ItemData MR_fetchAllGroupedBy:@"category.order"
                                                                withPredicate:predicate
                                                                     sortedBy:@"category.order"
                                                                    ascending:YES
                                                                     delegate:delegate];
      completion(controller);
}

但由于未捕获的异常'NSInvalidArgumentException'导致以下终止应用程序崩溃,原因:'此处不允许使用多对键'

如果有多对多的关系,我怎样才能更改它以按类别分组并显示与之关联的项目?

1 个答案:

答案 0 :(得分:0)

Each section create on fetch result controller:

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSInteger numberOfRows = 0;
    if (section==0)
    {
        NSArray *sections =[self.sample_fetchedResultsController sections];
        id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:0];
        numberOfRows = [sectionInfo numberOfObjects];
        return numberOfRows;
    }
    else
    {
        NSArray *sections =[self.fetchedResultsController sections];
        id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:0];
        numberOfRows = [sectionInfo numberOfObjects];
        return numberOfRows;

    }
}

    - (NSFetchedResultsController*)sample_fetchedResultsController
    {
        // if allready created return the created controller;
        if (_sample_fetchedResultsController) return _sample_fetchedResultsController;

        // create a managed object context
        NSManagedObjectContext *context = [[CoreDataController sharedInstance] objectContext];

        // create fetch request for demo entities
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"MySample"  inManagedObjectContext:context];

        [fetchRequest setEntity:entity];


        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"friendshipStatus == %@", [NSNumber numberWithInteger:MyCondition]];
        [fetchRequest setPredicate:predicate];

        // set sort properties

        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES selector:@selector(caseInsensitiveCompare:)];

        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
        [fetchRequest setSortDescriptors:sortDescriptors];

        [fetchRequest setFetchBatchSize:20];

        // create a new NSFetchedResultsController
        _friends_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                             managedObjectContext:context
                                                               sectionNameKeyPath:nil
                                                                        cacheName:nil];
        //                                                   cacheName:@"Root"];
        // delegate events to self
        [_sample_fetchedResultsController setDelegate:self];


        return _sample_fetchedResultsController;
    }



    Implement fecthedresultcontroller delegates and reload the collection view
    for example : 
    - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
    {
        [self.collectionView reloadData];
        self.collectionView.scrollEnabled = true;

    }