简体   繁体   中英

Get distinct values from parse.com

I've got a parse.com back-end with some tables:

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

So shopHasProduct is a many-to-many table.

What I need, is to get all the product that is not in shopHasProduct table for a specific shop .

What i've done so far is (query is on shopHasProduct table):

[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

in viewDidLoad I do the following:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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