I am a noob when it comes to traversing Core Data many-to-many relationships (I have read numerous threads and documentation on this so unless it’s and end all, please no links to documentation or threads).
I am making an inventory application and currently have a Core Data model that includes an “Thing”, “Category”, “Location”, and “ThingLocation” (ThingLocation is an entity that holds both a Thing and Location reference but includes the amount of Things on that particular Location. Also a many-to-many relationship) Entities that I would like to populate my UI with. I am proficient in GUI so this is not a question of User Interface but rather how I would gather the information using (probably) NSPredicates.
Ex: If I show a TableView consisting of a Category entity’s details then how would I populate it with the Things in that Category Entity.
Ex: If I wanted to display a UILabel showing the total amount of Thing‘s there were in it. (i.e. add up all of the amounts on each Location).
EDIT: I want to be able to use an NSFetchedResultsController!
I am not exactly sure what your question is asking. So for example you want to iterate over all the categories and all the things in that category you would first do a request for the entities of category without a predicate (this will return all category objects) and iterate over all those with fast enumeration:
For your first example of populating a tableview with things in a category,
If you have the category you would get the Things very easily like this:
You can iterate over this in a very normal fashion or use them as your datasource for your tableview. If you don’t have the category you need something to distinguish it in which case you would set up the predicate like this:
This will return all the Things that are connected to a category that has that attribute.
For your second example, You would set the NSPredicate up to get all the ThingLocations for that specific Thing. Then iterate over them and add up the values at the locations. If you wanted to do this for every category over everything it would just require you to nest some for loop starting with the categories. then for each thing get all the ThingLocations and for each of those add up the values.
I hope that answers your questions. To-Many relations are just sets and can be treated as such. I find that thinking from the bottom up helps me form the predicates. thinking I need all the things in this category so I would set up with the entity of Things and connecting it back to the category in the predicate.
Edit: NSFetchedResultsController example
In your .h file after declaring your super class add
NSFetchedResultsControllerDelegateto the delegates implemented.Create an ivar:
@property (nonatomic, retain) NSFetchedResultsController* fetchedResultsController;
On the implementation side I’ve seen two different approaches, the first is writing a custom accessor for the property that initializes it there, the other is just to do it in the
viewDidLoadin either case the setup is as follows:Then for the table view delegate methods it is a piece of cake like so:
You also need to add the delegate methods for the fetched results controller. They are all very simple and look something like this:
This will update the table if in some other part of the program a thing is added it will automatically show up on the table if the predicate matches.