简体   繁体   English

从parse.com获取不同的值

[英]Get distinct values from parse.com

I've got a parse.com back-end with some tables: 我有一个带有一些表的parse.com后端:

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

So shopHasProduct is a many-to-many table. 因此shopHasProduct是一个多对多表。

What I need, is to get all the product that is not in shopHasProduct table for a specific shop . 我需要的是获取特定shop shopHasProduct表中没有的所有product

What i've done so far is (query is on shopHasProduct table): 到目前为止,我所做的是(查询在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;

That gets me all the products, that are not in the current shop. 那让我得到了当前商店中没有的所有产品。 But the products are there for each shop, so they are reoccurring. 但是每家商店都有产品,因此它们不断发生。

How do I sort them so they are distinct? 我该如何对它们进行分类以使其与众不同?

I ended up not using PFQueryTableViewController but a normal UITableViewController 我最终没有使用PFQueryTableViewController而是一个普通的UITableViewController

in viewDidLoad I do the following: 在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];
}];

So I get an NSMutableArray with only distinct products. 因此,我得到的NSMutableArray仅包含独特的产品。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM