aaronium112
aaronium112

Reputation: 3088

How to write an NSPredicate For CoreData to-many relationship

I have 2 levels of UITableViews in a Navigation Hierarchy. On the first page is a UITableView that displays all the instances of Equipment using a NSFetchedResultsController which works great. On the second page I want to get back all the Note entities for a specific piece of Equipment so I can show them in a tableView.

How do I write a NSPredicate to only return the relevant notes?

Table Relationship

- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    //Init Fetch Request
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    //Entity the NSFetchedResultsController will be managing
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Note" 
                                              inManagedObjectContext:self.myContext];
    [fetchRequest setEntity:entity];

    //Sort Descriptors
    NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"noteTime" 
                                                                    ascending:YES];//Primary Sort
    NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"noteText" 
                                                                    ascending:YES];//Secondary Sort
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, 
                                                                sortDescriptor2, 
                                                                nil];

    //Set Sort Descriptors
    [fetchRequest setSortDescriptors:sortDescriptors];

    // limit Fetch to notes that belong to myEquipment
    //self
   // NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"item.name like '%@'", self.item.name]];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:PREDICATE GOES HERE];
    [fetchRequest setPredicate:predicate];



    [fetchRequest setFetchBatchSize:10];

    NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest    
                                                                          managedObjectContext:self.myContext 
                                                                            sectionNameKeyPath:nil 
                                                                                     cacheName:@"Equipment"];
    //Set the Delegate Property
    frc.delegate = self;
    _fetchedResultsController = frc; 

    //Release Local Memory
    [sortDescriptor1 release];
    [sortDescriptor2 release];
    [sortDescriptors release];
    [fetchRequest release];

    return _fetchedResultsController;
} 

Upvotes: 2

Views: 3967

Answers (1)

Brent Priddy
Brent Priddy

Reputation: 3847

Take a look at the core data documentation and the predicate programming guide you should see that you have to do something like

Equipment *equipment = // your equipment was passed from the previous tableview controller
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"equipment == %@", equipment];

Upvotes: 4

Related Questions