从parse.com获取不同的值

时间:2013-11-06 17:09:46

标签: ios parse-platform

我有一个parse.com后端有一些表格:

shop
product
shopHasProduct
  Shop:Shop Pointer;
  Product:Product pointer; 
  + some fields relation to shop-specific info

所以shopHasProduct是一个多对多的表。

我需要的是获取特定product不在shopHasProduct表格中的所有shop

到目前为止我所做的是(查询在shopHasProduct表上):

[query whereKey:@"Shop" notEqualTo:[[PFUser currentUser] objectForKey:@"currentShop"]];
[query includeKey:@"Product"];

PFQuery *shopHasProductThisShop = [[PFQuery alloc] initWithClassName:@"shopHasProduct"];
[shopHasProductThisShop whereKey:@"Shop" equalTo:[[PFUser currentUser] objectForKey:@"currentShop"]];

PFQuery *finalQ = [[PFQuery alloc] initWithClassName:@"shopHasProduct"];
[finalQ whereKey:@"Product" doesNotMatchKey:@"Product" inQuery:shopHasProductThisShop];
[finalQ includeKey:@"Product"];

return finalQ;//query;

这让我得到了所有产品,而不是现在的商店。但是每个商店都有产品,所以它们会重复出现。

如何对它们进行排序以使它们不同?

1 个答案:

答案 0 :(得分:-2)

我最终没有使用PFQueryTableViewController,而是使用普通UITableViewController

在viewDidLoad中我执行以下操作:

PFQuery *shopHasProductThisShop = [[PFQuery alloc] initWithClassName:@"shopHasProduct"];
[shopHasProductThisShop whereKey:@"Shop" equalTo:[[PFUser currentUser] objectForKey:@"currentShop"]];



PFQuery *hej = [[PFQuery alloc] initWithClassName:@"shopHasProduct"];
[hej whereKey:@"Product" doesNotMatchKey:@"Product" inQuery:shopHasProductThisShop];
[hej includeKey:@"Product"];

[hej findObjectsInBackgroundWithBlock:^(NSArray *allProducts, NSError *error) {

    self.productToAdd = [NSMutableArray new];
    self.productArray = [NSMutableArray new];
    NSLog(@"Hvor mange er der i alle prodikter'et:%d", [allProducts count]);

    for (PFObject *object in allProducts) {

        PFObject *random = object[@"Product"];
        [self.productArray addObject:random];

    }
    NSSet *uniqueStates = [NSSet setWithArray:[self.productArray valueForKey:@"objectId"]];

    NSArray *foobar = [uniqueStates allObjects];
    for (NSString *string in foobar) {
        PFObject *hulo = [PFQuery getObjectOfClass:@"Product" objectId:string];
        [self.productToAdd addObject:hulo];
    }  
   [self.tableView reloadData];
}];

所以我得到的NSMutableArray只包含不同的产品。