Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Swift - Core Data examples with a complex data relationship [closed]

I'm trying to understand how to model complex data relationships with Core Data.

With my app, I currently have an entity of Recipe, Ingredient and RecipeIngredient which bind ingredients to a recipe.

I have not come across any example of fetching data out of this joint entity. I'd appreciate it if someone could give an example of an entity like my RecipeIngredient in Swift.

like image 806
Seong Lee Avatar asked Mar 24 '15 08:03

Seong Lee


Video Answer


1 Answers

The reason you haven't seen examples similar to your RecipeIngredient entity is that you don't need joint entities like that in Core Data. You're treating Core Data as if it were a relational database, where you'd typically use a join table in order to create efficient many-to-many relationships between entities. As explained in the Many-to-Many Relationships sub-section of the Core Data Programming Guide, with Core Data all you need to do is to specify a to-many relationship in both directions between two entities. Note the parenthetical remark in the docs:

(If you have a background in database management and this causes you concern, don't worry: if you use a SQLite store, Core Data automatically creates the intermediate join table for you.)

Here's an illustration of the relationship as you should model it, ripped straight from Xcode's model editor:

many-to-many

If you'd still like to see examples of how to do this, search for something like "Core Data many to many relationships" and you'll find plenty. You could start here on StackOverflow; a quick search turned up a number of examples, including How do you manage and use "Many to many" core data relationships?.

Update: From your comment, I understand that you want to use an intermediate object to add information about the relationship between recipes and ingredients. That is a case where another entity is warranted. So let's say your model looks like this:

updated model

It seems unlikely that you'd want to fetch one of these RecipeIngredient objects directly; you'd probably just follow the appropriate relationship. So, you might create a fetch request to find all the Recipes whose name matches @"chocolate cake". (There are plenty of examples of fetch requests using a predicate in the docs and all over the net, so I won't do that here.) Your fetch request will return an array of recipes that we could call cakeRecipes, but you're probably only interested in one:

Recipe *cake = cakeRecipes.firstObject;

Now, what do you want to know about the ingredients for your cake? Here's a list of the ingredients:

NSArray *ingredientNames = cake.ingredients.ingredient.name;

If you'd like to log the ingredient names and amounts:

for (RecipeIngredient *i in cake.ingredients) {
    NSLog(@"%@ %@", i.amount, i.ingredient.name);
}

Or, you could use a fetch request to find the ingredients matching "celery", storing the result in celeries. After that, you might look for recipes including celery:

Ingredient *celery = celeries.firstObject;
NSArray *recipes = celery.recipes.recipe

If this doesn't help, perhaps you could be more specific about the problem. Also, I know you asked for Swift, but my fingers are still used to Obj-C, and the language specifics don't really come into play here -- Core Data works the same in both languages.

like image 197
Caleb Avatar answered Sep 27 '22 23:09

Caleb