我如何返回与食谱相关的所有成分,特别是来自属于事件的食谱?然后通过过道订购它们?
进一步解释:
用户有食谱对象的“食谱书”,它与成分有关系。当用户计划准备食谱时,用户会将食谱与事件相关联。我想要提取的数据是属于食谱的成分列表,属于事件,由Aisle订购。基本上是一个“购物清单”。
有关Event对象的进一步说明: 事件允许重复食谱。假设我连续3天(3个单独的活动)要吃一个煎蛋(成分=“1个新鲜鸡蛋”),我希望购物清单包含“1个新鲜鸡蛋”三次 - 每个活动一个实例。 / p>
这是我的获取请求,它没有返回任何结果 - 我认为问题的关键是谓词:
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:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
self.fetchedResultsController = frc;
感谢Martin R的提问,我进一步解决了这个问题。以下是一些示例数据和所需的输出:
Z_PK = 1,ZTITLE =“燕麦片”
Z_PK = 2,ZTITLE =“花生酱和果冻三明治”
Z_PK = 3,ZTITLE =“烤芝士三明治”
Z_PK = 4,ZTITLE =“炖牛肉”
Z_PK = 5,ZTITLE =“汉堡”
...还有更多不包含在获取结果中的食谱...
Z_PK = 1,ZTITLE =“早餐1”,ZRECIPE = 1
Z_PK = 2,ZTITLE =“午餐1”,ZRECIPE = 2
Z_PK = 3,ZTITLE =“晚餐”,ZRECIPE = 4
Z_PK = 4,ZTITLE =“早餐2”,ZRECIPE = 1 //注意重复配方
Z_PK = 5,ZTITLE =“午餐2”,ZRECIPE = 3
Z_PK = 6,ZTITLE =“晚餐2”,ZRECIPE = 5
Z_PK = 1,ZTITLE =“1杯燕麦片”,ZRECIPE = 1,ZAISLE = 4
Z_PK = 2,ZTITLE =“2杯牛奶”,ZRECIPE = 1,ZAISLE = 2
Z_PK = 3,ZTITLE =“2片白面包”,ZRECIPE = 2,ZAISLE = 5
Z_PK = 4,ZTITLE =“1汤匙果冻”,ZRECIPE = 2,ZAISLE = 4
Z_PK = 5,ZTITLE =“1汤匙花生酱”,ZRECIPE = 2,ZAISLE = 4
Z_PK = 6,ZTITLE =“2件黑麦面包”,ZRECIPE = 3,ZAISLE = 5
Z_PK = 7,ZTITLE =“1奶酪片”,ZRECIPE = 3,ZAISLE = 2
Z_PK = 8,ZTITLE =“1磅。卡盘烤”,ZRECIPE = 4,ZAISLE = 3
Z_PK = 9,ZTITLE =“2胡萝卜”,ZRECIPE = 4,ZAISLE = 1
Z_PK = 10,ZTITLE =“2个土豆”,ZRECIPE = 4,ZAISLE = 1
Z_PK = 11,ZTITLE =“1个汉堡面包”,ZRECIPE = 5,ZAISLE = 5
Z_PK = 12,ZTITLE =“1/4磅汉堡”,ZRECIPE = 5,ZAISLE = 3
Z_PK = 1,ZTITLE =“生产”,ZDISPLAYORDER = 1
Z_PK = 2,ZTITLE =“Dairy”,ZDISPLAYORDER = 2
Z_PK = 3,ZTITLE =“Meats”,ZDISPLAYORDER = 3
Z_PK = 4,ZTITLE =“干燥和罐装物品”,ZDISPLAYORDER = 4
Z_PK = 5,ZTITLE =“Bakery”,ZDISPLAYORDER = 5
生产
-2胡萝卜
-2土豆
的乳制品
-2杯牛奶
-2杯牛奶//第二项,因为有两个配方实例
-1奶酪片
的肉类
-1磅查克烤肉
-1/4磅汉堡包
干燥和罐装物品
-1杯燕麦片
-1杯燕麦片//第二项,因为有两个配方实例
-1汤匙。果冻
-1汤匙花生酱
面包
-2片白面包
-2片黑麦面包
-1汉堡包
答案 0 :(得分:0)
我认为你想要一个谓词:
recipe.events.@count > 0
这会让Ingredients
用于任何Recipes
Event
。
或者,如果您正在寻找特定Ingredients
的{{1}}中使用的Recipe
,那就是:
Event
您的排序描述符可能是[NSPredicate predicateWithFormat:@"ANY recipe.events = %@", specificEvent]
,而不仅仅是aisle.displayOrder
。
答案 1 :(得分:0)
获取请求不能(据我所知)返回同一个托管对象的重复条目。所以我最接近的解决方案如下。
取出所有成分并根据aisle.displayOrder
将它们分成几部分。在每个部分中,按照自己的显示顺序对成分进行排序:
NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:@"aisle.displayOrder" ascending:YES];
NSSortDescriptor *sort2 = [NSSortDescriptor sortDescriptorWithKey:@"displayOrder" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObjects:sort1, sort2, nil];
NSFetchedResultsController *controller = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:@"aisle.displayOrder"
cacheName:@"fetchControllerCache"];
对于每种成分,您可以计算事件数量(并在表格中显示):
int numberOfEvents = theIngredient.recipe.events.count;