Skip to content

Combining Conditions

cmancushman edited this page Oct 27, 2017 · 4 revisions

The Purpose of Combining Conditions

A condition by itself is a useful tool for sifting data, but what if we want data that satisfy multiple criteria? Combined conditions are helpful in creating complex searches.


Conditions That Checks if Multiple Conditions are All Satisfied

Declaration

+(instancetype) conditionsAreMet:(NSArray<StackBaseCondition *> *)conditions;

Example

//get the first 3 rows where columns 'Name' and 'Memo' contain matches for the phrase 'Matt' and also where column 'id' is greater than 10 
StackBaseCondition *columnsMatchPhrase = [StackBaseCondition columnsWithNames:@[@"Name", @"Memo"] matchPhrase:@"Sean"];
    
StackBaseCondition *idIsBigEnough = [StackBaseCondition columnWithName:@"id" isGreaterThan:@10];

[weakSelf.table getFirst:3 
rowsWhere:[StackBaseCondition conditionsAreMet:@[columnsMatchPhrase, idIsBigEnough]]
completionBlock:^(BOOL success, NSString *responseMessage, NSArray<NSDictionary *> *responseTable) {
    
    for(NSDictionary *row in responseTable){
        
        NSLog(@"row %ld: %@", ([responseTable indexOfObject:row] + 1), row);
        
    }
    
}];

Conditions That Checks if at Least One of Multiple Conditions are Satisfied

Declaration

+(instancetype) oneOfTheseConditionsAreMet:(NSArray<StackBaseCondition *> *)conditions;

Example

//get the first 3 rows where column 'id' is less than 50 or where column 'id' is greater than 100 
StackBaseCondition *idIsSmallEnough = [StackBaseCondition columnWithName:@"id" isLessThan:@50];
    
StackBaseCondition *idIsBigEnough = [StackBaseCondition columnWithName:@"id" isGreaterThan:@100];

[weakSelf.table getFirst:3 
rowsWhere:[StackBaseCondition oneOfTheseConditionsAreMet:@[idIsSmallEnough, idIsBigEnough]]
completionBlock:^(BOOL success, NSString *responseMessage, NSArray<NSDictionary *> *responseTable) {
    
    for(NSDictionary *row in responseTable){
        
        NSLog(@"row %ld: %@", ([responseTable indexOfObject:row] + 1), row);
        
    }
    
}];

Note On Combining Conditions

Although these methods exist, not all conditions can be combined. The conditionsAreMet: condition can be combined with other methods. However, the oneOfTheseConditionsAreMet: condition can never be combined with another condition. If it is, the resulting condition will return nil and a description of the problem will be logged.

To be clear, the following theoretical declaration would be okay:

[StackBaseCondition oneOfTheseConditionsAreMet:@[[StackBaseCondition conditionsAreMet:@[...]], otherCondition]];

This, on the other hand, would return a null value:

[StackBaseCondition conditionsAreMet:@[[StackBaseCondition oneOfTheseConditionsAreMet:@[...]], otherCondition]];

Clone this wiki locally