我想用作我的应用程序Parse的后端,并假设我有一个不同餐厅的课程,每个餐厅的菜单和每个菜单不同的产品,我有一个类Place,Menu,Product和MenuItems:< / p>
MenuItem类具有: Pointe菜单 指针产品
菜单类: 指针位置
一旦选择餐厅必须显示该餐厅的所有产品: 我的代码:
PFQuery *query = [PFQuery queryWithClassName:@"Places"];
[query whereKey:@"name" equalTo:PlaceSelect];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
PFObject *Menu;
if (!error)
{
for (PFObject *ob in objects)
{
Menu = ob;
}
}
PFQuery *query1 = [PFQuery queryWithClassName:@"Menus"];
[query1 whereKey:@"place" equalTo:Menu];
[query1 findObjectsInBackgroundWithBlock:^(NSArray *objects2, NSError *error2) {
PFObject *MenuItems;
if (!error2)
{
for (PFObject *ob2 in objects2)
{
MenuItems = ob2;
}
}
PFQuery *query2 = [PFQuery queryWithClassName:@"MenuItems"];
[query2 whereKey:@"menu" equalTo:MenuItems];
[query2 selectKeys:@[@"product"]];
[query2 findObjectsInBackgroundWithBlock:^(NSArray *objects3, NSError *error3) {
PFObject *Products;
if (!error3)
{
for (PFObject *ob3 in objects3)
{
Products = ob3;
NSLog(@" %@ ",Products);
}
我得到相关产品,但是当我想按类型过滤时:饮料,开胃菜等......它给了我所有地方的所有产品......
PFQuery *retrievedDrink = [PFQuery queryWithClassName:@"Products"];
[retrievedDrink whereKey:@"type" equalTo:@"drink"];
[retrievedDrink whereKey:@"objectId" equalTo:Products.objectId];
[retrievedDrink findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
DrinksArray = [[NSArray alloc] initWithArray:objects];
NSLog(@"numero de productos Drinks= %i ",[DrinksArray count]);
}];
在此先感谢,如果有人知道更有效率的方法请我澄清我的怀疑
答案 0 :(得分:0)
摆脱这条线
[retrievedDrink whereKey:@"objectId" equalTo:Products.objectId];
即返回所有对象。
您有一个不需要的重复对象数组,并且还要进行错误检查。
这应该找到存储在Parse类中的所有饮料,将它们存储在名为objects
的数组中,并允许您在findObjectsInBackgroundWithBlock
PFQuery *retrievedDrink = [PFQuery queryWithClassName:@"Products"];
[retrievedDrink whereKey:@"type" equalTo:@"drink"];
[retrievedDrink findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(@"numero de productos Drinks= %i ",[objects count]);
}
}];