Question

i am using this link as a guide, but i can figure out how can i use AND clause for parse. It only defines OR clause in sample code.

if i were to convert it from SQL statement, it looks like this...

SELECT * FROM RemittanceTable WHERE beneCode = 'code' AND remittanceAmount = 500.00

For example in this code, i want to check if the code AND amount if correct before reading the data. But it validates with OR clause

PFQuery *beneCodeQuery = [PFQuery queryWithClassName:@"RemittanceTable"];
[beneCodeQuery whereKey:@"beneCode" containsString:_beneCodeText.text];

PFQuery *amountQuery = [PFQuery queryWithClassName:@"RemittanceTable"];
[amountQuery whereKey:@"remittanceAmount" equalTo:amount];

PFQuery *query = [PFQuery orQueryWithSubqueries:[NSArray arrayWithObjects:beneCodeQuery,amountQuery,nil]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        for (PFObject *object in objects) {
            NSLog(@"%@", object);

            [self resignAllResponder];
            _paymentDetailsImage.frame = CGRectMake(_paymentDetailsImage.frame.origin.x, _paymentDetailsImage.frame.origin.y, _paymentDetailsImage.frame.size.width, paymentModeOrigHeight);
            _detailsView.alpha = 1;

        }
    }}}];
Was it helpful?

Solution

Each time you add a constraint, it is implicitly using AND:

[query whereKey:@"username" equalTo:@"admin"];
[query whereKey:@"location" equalTo:@"USA"];

translates to "username == 'admin' AND location == 'USA'"

UPDATE

Your specific case as an example:

PFQuery *beneCodeQuery = [PFQuery queryWithClassName:@"RemittanceTable"];
[beneCodeQuery whereKey:@"beneCode" containsString:_beneCodeText.text];
[beneCodeQuery whereKey:@"remittanceAmount" equalTo:amount];

NSArray *result = [beneCodeQuery findObjects];

OTHER TIPS

I came across this answer by Héctor Ramos on Parse.com forum recently:

You can indeed have multiple constraints on a single query, which will be treated as an AND. Having the same constraint more than once for the same key, is not supported. In this case you have two whereKey:equalTo: constraints over "members", so only the last one is being considered.

Instead you'll want to use whereKey:containsAllObjectsInArray:.

PFQuery *query = [PFQuery queryWithClassName:@"Conversation"];
[query whereKey:@"members" containsAllObjectsInArray:@[aFriend.data, [PFUser currentUser]]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {...}];

I hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top