Baub
Baub

Reputation: 5044

Fetch a To-Many Entity

I have two entities pictured below. How do I fetch all of the Nodes for a certain Trip? And how do I specify the Trip that I want to fetch?

enter image description here

Upvotes: 0

Views: 44

Answers (1)

isaac
isaac

Reputation: 4897

Assuming you have a managed object context, you'll fetch a group of of Trips using a fetched results controller. You can place a predicate on a fetch, and you'll get back only the record(s) that you've matched. You'll need to review the documentation on NSFetchedResultsController to learn how to obtain the Trip you want from CoreData.

Once you have a Trip, it's easy to get your nodes. Call:

[trip nodesInTrip]

You'll get a set back.

If you're using a table view, it's even easier (this assumes your fetchedResultsController is your table views data source):

-(void)didSelectRowAtIndexPath:indexPath {

    Trip *selectedTrip = [[fetchedResultsController fetchedObjects] objectAtIndexPath:indexPath];
    NSArray *arrayOfNodes = [selectedTrip nodesInTrip];

Upvotes: 1

Related Questions