0

在此处输入图像描述

我将如何返回与食谱相关的所有成分,特别是从属于事件的食谱中?然后按过道订购?

进一步说明:

用户有一个食谱对象的“食谱书”,它与成分有关系。用户在计划准备食谱时会将食谱与事件相关联。我试图提取的数据是属于食谱的成分列表,属于事件,由 Aisle 排序。基本上是一个“杂货清单”。

关于 Event 对象的进一步解释:Events 允许重复的配方。假设我要连续 3 天(3 个单独的事件)吃一个煎鸡蛋(成分 =“1 个新鲜鸡蛋”),我希望购物清单包含 3 次“1 个新鲜鸡蛋”——每个事件一个实例。

这是我的 fetch 请求,它没有返回任何结果 - 我认为问题的症结在于谓词:

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 = "Lunch 2", ZRECIPE = 3
Z_PK = 6, ZTITLE = "Dinner 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 = "乳制品", ZDISPLAYORDER = 2
Z_PK = 3, ZTITLE = "肉类", ZDISPLAYORDER = 3
Z_PK = 4, ZTITLE = "干货和罐头食品", ZDISPLAYORDER = 4
Z_PK = 5, ZTITLE = "面包店", ZDISPLAYORDER = 5

期望的输出(按 Aisle ZDISPLAYORDER 排序,粗体项目为章节标题)

生产
-2 胡萝卜
-2 土豆
乳制品
-2 杯牛奶
-2 杯牛奶 // 第二项,因为配方有两个实例
-1 奶酪片
肉类
-1 磅。夹头烤
-1/4 磅汉堡
干货和罐头食品
-1 杯燕麦片
-1 杯燕麦片//第二道菜,因为有两个配方实例
-1 汤匙。果冻
-1 汤匙花生酱
面包店
-2 块白面包
-2 块黑麦面包
-1 汉堡包

4

2 回答 2

0

我想你会想要一个谓词:

recipe.events.@count > 0

这会让你Ingredients在任何适用Recipes于任何Event.

或者,如果您正在寻找用于Ingredients任何Recipe特定的Event,那将是:

[NSPredicate predicateWithFormat:@"ANY recipe.events = %@", specificEvent]

您的排序描述符可能是aisle.displayOrder,而不仅仅是aisle.

于 2012-08-23T18:05:03.240 回答
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;
于 2012-08-23T19:24:00.727 回答