Note that there are some explanatory texts on larger screens.

plurals
  1. POHow would I form a fetch result predicate based on this data model?
    primarykey
    data
    text
    <p><img src="https://i.stack.imgur.com/ZuO9q.jpg" alt="enter image description here"></p> <p>How would I return all the ingredients related to recipes, specifically from recipes that belong to events? And then order them by aisle?</p> <p>Further explanation:</p> <p>User has a "recipe book" of Recipe objects, which has a relationship to Ingredients. The user will associate a Recipe to an Event when they plan on preparing the Recipe. The data I'm trying to pull out is the list of Ingredients that belong to Recipes, that belong to Events, ordered by Aisle. Basically a "grocery list".</p> <p>Further explanation on what an Event object is: Events allow for duplicate recipes. Say I am going to have a fried egg (ingredient = "1 fresh egg") for 3 consecutive days (3 separate events), I want the shopping list to contain "1 fresh egg" three times - one instance for each event.</p> <p>Here is my fetch request, which is not returning any results - I assume the crux of the issue is the predicate:</p> <pre><code>NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Ingredient" inManagedObjectContext:[self managedObjectContext]]; [request setEntity:entity]; // get ingredients of recipes that belong to events [request setPredicate:[NSPredicate predicateWithFormat:@"Ingredient.recipe == Event.recipe"]]; // sort by "aisle" NSSortDescriptor *sortDescriptorCategory = [NSSortDescriptor sortDescriptorWithKey:@"aisle" ascending:YES]; request.sortDescriptors = [NSArray arrayWithObjects:sortDescriptorCategory, nil]; // create nsfrc with "aisle" as sectionNameKeyPath NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"aisle" cacheName:@"MyFRCCache"]; frc.delegate = self; NSError *error = nil; if (![frc performFetch:&amp;error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } self.fetchedResultsController = frc; </code></pre> <p>Thanks to Martin R's question, I have further fleshed out the problem. Here is some sample data and the desired output:</p> <h2>Recipe</h2> <p>Z_PK = 1, ZTITLE = "Oatmeal"<br> Z_PK = 2, ZTITLE = "Peanut Butter and Jelly Sandwich"<br> Z_PK = 3, ZTITLE = "Grilled Cheese Sandwich"<br> Z_PK = 4, ZTITLE = "Beef Stew"<br> Z_PK = 5, ZTITLE = "Hamburger"<br> ... Plus more recipes that wouldn't be included in fetch results ...</p> <h2>Event</h2> <p>Z_PK = 1, ZTITLE = "Breakfast 1", ZRECIPE = 1<br> Z_PK = 2, ZTITLE = "Lunch 1", ZRECIPE = 2<br> Z_PK = 3, ZTITLE = "Dinner ", ZRECIPE = 4<br> Z_PK = 4, ZTITLE = "Breakfast 2", ZRECIPE = 1 // Note repeat of recipe<br> Z_PK = 5, ZTITLE = "Lunch 2", ZRECIPE = 3<br> Z_PK = 6, ZTITLE = "Dinner 2", ZRECIPE = 5 </p> <h2>Ingredient</h2> <p>Z_PK = 1, ZTITLE = "1 cup oatmeal", ZRECIPE = 1, ZAISLE = 4<br> Z_PK = 2, ZTITLE = "2 cups milk", ZRECIPE = 1, ZAISLE = 2<br> Z_PK = 3, ZTITLE = "2 pieces white bread", ZRECIPE = 2, ZAISLE = 5<br> Z_PK = 4, ZTITLE = "1 tbsp. jelly", ZRECIPE = 2, ZAISLE = 4<br> Z_PK = 5, ZTITLE = "1 tbsp peanut butter", ZRECIPE = 2, ZAISLE = 4<br> Z_PK = 6, ZTITLE = "2 pieces rye bread", ZRECIPE = 3, ZAISLE = 5<br> Z_PK = 7, ZTITLE = "1 cheese slice", ZRECIPE = 3, ZAISLE = 2<br> Z_PK = 8, ZTITLE = "1 lbs. chuck roast", ZRECIPE = 4, ZAISLE = 3<br> Z_PK = 9, ZTITLE = "2 carrots", ZRECIPE = 4, ZAISLE = 1<br> Z_PK = 10, ZTITLE = "2 potatoes", ZRECIPE = 4, ZAISLE = 1<br> Z_PK = 11, ZTITLE = "1 hamburger bun", ZRECIPE = 5, ZAISLE = 5<br> Z_PK = 12, ZTITLE = "1/4 pound hamburger", ZRECIPE = 5, ZAISLE = 3 </p> <h2>Aisle</h2> <p>Z_PK = 1, ZTITLE = "Produce", ZDISPLAYORDER = 1<br> Z_PK = 2, ZTITLE = "Dairy", ZDISPLAYORDER = 2<br> Z_PK = 3, ZTITLE = "Meats", ZDISPLAYORDER = 3<br> Z_PK = 4, ZTITLE = "Dry &amp; Canned Items", ZDISPLAYORDER = 4<br> Z_PK = 5, ZTITLE = "Bakery", ZDISPLAYORDER = 5 </p> <h2>Desired Output (sorted by Aisle ZDISPLAYORDER, bolded items are section titles)</h2> <p><strong>Produce</strong><br> -2 carrots<br> -2 potatoes<br> <strong>Dairy</strong><br> -2 cups milk<br> -2 cups milk // second item because there are two instances of the recipe<br> -1 cheese slice<br> <strong>Meats</strong><br> -1 lbs. chuck roast<br> -1/4 pound hamburger<br> <strong>Dry and Canned Items</strong><br> -1 cup oatmeal<br> -1 cup oatmeal // second item because there are two instances of the recipe<br> -1 tbsp. jelly<br> -1 tbsp peanut butter<br> <strong>Bakery</strong><br> -2 pieces white bread<br> -2 pieces rye bread<br> -1 hamburger bun </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload