Progress will discontinue Telerik Platform on May 10th, 2018. Learn more

Filtering

Filtering

You can apply filters on any Create, Read, Update, and Delete data operations to alter the result of a query.

In the Backend Services iOS SDK, filtering is performed in a way very similar to filtering in Apple Core Data.

EVFetchRequest

EVFetchRequest just like the NSFetchRequest uses predicates. Instances of the NSPredicate class are used to define filtering expressions and operators.

Retrieving data with NSPredicate

In the Backend Services iOS SDK you can filter items passing NSPredicate to the EVFetchRequest. The supported operators are : ==, <, >, <=, >=, AND, OR, NOT, BEGINSWITH, ENDSWITH, CONTAINS, MATCHES, BETWEEN, LIKE, IN. When describing filter expressions you may use the standard NSPredicate syntax. Below you may find some examples.

Value equal to

EVFetchRequest *request = [EVFetchRequest fetchRequestWithKindOfClass:[Activities class]];
NSString *text = @"It is finally time for graduation! Good job everyone, we made it.!";
[request setPredicate:[NSPredicate predicateWithFormat:@"Text == %@",text]];

[dataStore executeFetchRequest:request block:^(NSArray *result, NSError *error) {
    //here result will contain the fetched Activities
}];

Value not equal to

EVFetchRequest *request = [EVFetchRequest fetchRequestWithKindOfClass:[Activities class]];
NSString *text = @"It is finally time for graduation! Good job everyone, we made it.!";
[request setPredicate:[NSPredicate predicateWithFormat:@"Text != %@",text]];

[dataStore executeFetchRequest:request block:^(NSArray *result, NSError *error) {
    //here result will contain the fetched Activities
}];

Value compared to

By executing the following code snippet, you can get all items which value is greater than a certain constant:

NSDate *date; // set date here.
EVFetchRequest *request = [EVFetchRequest fetchRequestWithKindOfClass:[Activities class]];
[request setPredicate:[NSPredicate predicateWithFormat:@"ModifiedAt > %@",date]];
[dataStore executeFetchRequest:request block:^(NSArray *result, NSError *error) {
    //here result will contain the fetched Activities                
}];

Value in array of possible matches

NSMutableArray* array = [[NSMutableArray alloc]init];
[array addObject:[[NSNumber alloc] initWithInt:1 ]];
[array addObject:[[NSNumber alloc] initWithInt:42 ]];

NSPredicate *predicate =[NSPredicate predicateWithFormat: @"quantity IN %@", array];
EVFetchRequest *request = [EVFetchRequest fetchRequestWithKindOfClass:[Items class]];

[request setPredicate:[NSPredicate predicateWithFormat: @"quantity IN %@", array]];
[dataStore executeFetchRequest:request block:^(NSArray *result, NSError *error) {
    //here result will contain the fetched Items which have quantity with value present in the provided array.
}];

Compound predicates (AND, OR, NOT)

You can combine multiple filter expressions to get items. For example, to get items, which meets a list of criteria, you have to execute the following code snippet:

NSPredicate *predicate =[NSPredicate predicateWithFormat: @"(quantity < 100) AND (quantity > 10)"];
NSPredicate *predicate =[NSPredicate predicateWithFormat: @"(firstName = %@) OR (firstName = %@)",@"John",@"Jane"];
NSPredicate *predicate =[NSPredicate predicateWithFormat: @"NOT((age < 18) AND (age > 80))"];

Examples with String expressions

Here are some examples with string expressions:

NSPredicate *predicate =[NSPredicate predicateWithFormat: @"FirstName BEGINSWITH Jo"];
NSPredicate *predicate =[NSPredicate predicateWithFormat: @"FirstName ENDSWITH hn"];
NSPredicate *predicate =[NSPredicate predicateWithFormat: @"FirstName CONTAINS ohn"];

Case Insensitive filtering

String comparisons are by default case sensitive. You can make the filtering case insensitive by using the key character c within square braces.

NSPredicate *predicate =[NSPredicate predicateWithFormat: @"FirstName BEGINSWITH[c] jo"];
NSPredicate *predicate =[NSPredicate predicateWithFormat: @"FirstName ENDSWITH[c] HN"];
NSPredicate *predicate =[NSPredicate predicateWithFormat: @"FirstName CONTAINS[c] oHn"];
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.