I have two entities, one called "Recipe" and the other, "Ingredient". There is an one-to-many relationship ("recipeIngredients") between the two. My issue is I get nil when I try to retrieve the Ingredients from a fetched Recipe object. All the other Recipe object's attributes are retrieved just fine.
What I know/have:
- I set up the data model as stated in the Core Data tutorial on code.tutsplus.com. Really easy to create a one-to-many relationship; I don't think I messed this up, but there's always a chance. Relationship from Recipe to Ingredients is "recipeIngredients".
- Confirmed through debugging that the Ingredients are initialized properly since I see pointer addresses for them. These Ingredients are saved to a NSSet, which I save to the Recipe object with the setValue: forKey: method. No errors upon saving.
Here's my code for saving:
NSSet *ingredients = [NSSet setWithObjects:newIngRecipe1, newIng2Recipe1, newIng3Recipe1, newIng4Recipe1, newIng5Recipe1, nil];
[newRecipe1 setValue:ingredients forKey:@"recipeIngredients"];
Here's my code for retrieving:
Recipe *selectedRecipe = (Recipe *)[self.fetchedResultsController objectAtIndexPath:indexPath];
NSSet *selectedRecipeIngredients = selectedRecipe.recipeIngredients;
I am not opposed to suggestions of other methods of saving ingredients for a Recipe object. I could make an attribute for Recipe to save an array of Ingredients to make things easier, but from my research, it seemed like the proper way was to use a relationship. If there are other more "proper" ways to go about it, I am glad to try them.
setRecpieIngredients:? - Wain