Craig Watkinson
Craig Watkinson

Reputation: 913

Mogenerator with NSFetchedResultsController - 0 sections

I am populating a UITableView from Core Data using an NSFetchedResultsController. The data is coming from an NSManagedObject subclass generated by Mogenerator called MenuItem. The MenuItem entity has a SectionID parameter that is an NSNumber and this is used to determine what section the item should be in within the table view.

I performed a test fetch on the data and confirmed that Core Data was populated correctly. All was fine.

The NSFetchedResultsController is created as follows and the sectionKeyNamePath is set to @"sectionId":

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MenuItem" inManagedObjectContext:self.persistenceController.moc];
[fetchRequest setEntity:entity];
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"sectionId" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:@"rowId" ascending:YES]];    
NSFetchedResultsController *frc = nil;
frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                          managedObjectContext:self.persistenceController.moc
                                            sectionNameKeyPath:@"sectionId"
                                                     cacheName:nil];

The problem (and solution):

With this code, the sections are not identified. The NSFetchedResultsController always returns 0. This worked previously when I created the NSMO subclass manually, so I figured it was something related to Mogenerator.

If I change the sectionNameKeyPath to be @"primitiveSectionId" then it works.

Hopefully this will help somebody in the future, but I don't understand what is going on here. Please could somebody explain why this fixes the problem?

Thanks

Upvotes: 0

Views: 59

Answers (1)

kevnm67
kevnm67

Reputation: 177

Why are you setting the FRC to nil? Try this (it works for me using an NSNumber Attribute):

With mogenerator you can get the entity using "[MenuItem entityName]" and access attributes using MenuItemAttributes.sectionId, for example.

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[MenuItem entityName]];
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:MenuItemAttributes.sectionId ascending:YES], [NSSortDescriptor sortDescriptorWithKey:MenuItemAttributes.rowID ascending:YES]];
NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                      managedObjectContext:self.persistenceController.moc
                                                                        sectionNameKeyPath:MenuItemAttributes.sectionId
                                                                                 cacheName:nil];

// Make sure you set your frc as the delegate
frc.delegate = self;

NSError *error = nil;
if (![fetcher performFetch:&error]) {
    /*
     * Did you execute the request?
     */
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}
NSLog(@"%lu", [frc.sections count]);

Upvotes: 0

Related Questions