Frage

Here's my scenario: I have a "cameras" table view and a "Rolls" table view. I want to click on a camera and load it's corresponding rolls. I can't seem to get this to work. What is the proper way to pass the selected camera core data object into the predicate of the rolls table view NSFetchedResultsController?

Camera entity

relationship : rolls

Destination :Roll

Inverse : No Inverse

Roll entity

relationship : camera

Destination :Camera

Inverse : No Inverse

from roll table view controller

- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
    return _fetchedResultsController;
}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"Roll" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                          initWithKey:@"desc" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

[fetchRequest setFetchBatchSize:20];    

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY camera == %@", self.selectedCamera.objectID];
[fetchRequest setPredicate:predicate];

NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                    managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil
                                               cacheName:nil];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;

return _fetchedResultsController;
}
War es hilfreich?

Lösung

Your relationships should each have an inverse (in this case, be inverse of each other). This allows Core Data to properly manage the relationship and allows you to set it conveniently while navigating it in both directions. It also properly represents the relationship.

The predicate should be:

NSPredicate *p = [NSPredicate predicateWithFormat:@"camera == %@", self.selectedCamera];

to use the camera relationship on the Roll entity and check that it contains the provided self.selectedCamera entity instance.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top