0
votes

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.

1
Is the relationship bi-directional? Have you generated managed object subclasses? Are you saving the context? Why don't you setRecpieIngredients:? - Wain
can you provide more code? I assume you save after creation? Any reason not to use NSManagedObject subclasses? - Ron
Thanks for your comments, guys. I ended up solving my own issue, but to answer your questions, yes the relationship is bi-directional, and I did generate managed object subclasses and save the context. What was strange was that the accessor methods did not generate the first time. I regenerated the subclass and this time they popped out. I can save and retrieve Ingredients like a charm now. - Catherine S.

1 Answers

0
votes

It must be the Stack Overflow magic, but I ended up solving my own question after I checked out Apple's own Recipe app example, which I got here: Apple's iPhone Core Data Recipe app example. I recommend it for anyone using Core Data. I noticed that the example Recipe app had accessor methods for setting Ingredients to the Recipe and mine did not, and those accessor methods should be auto-generated. I regenerated the NSManagedObject subclasses, and BOOM, there were the accessor methods to add recipe ingredients in the Recipe class. Using these methods saved the Ingredients properly, and I can retrieve them now. I did not have to make any changes with the way I was fetching.

I have no idea why the auto-generated subclasses didn't have the methods there in the first place, but I guess the answer is to make sure the methods are there, and if not, auto-generate again.