Skip to content

Data Query

Data service find operation can accept an argument with additional options instructing the server to sort data, retrieve paged results and return related child objects. The example below demonstrates the usage of the data query argument in versions 3.x and 5.x:

Version 3.x

QueryOptions *queryOptions = [QueryOptions new];
// set related columns
[queryOptions addRelated:@"address"];
// request sorting
NSArray *sortBy = @[@"name"];
[queryOptions setSortBy:sortBy];
// set page size and offset
[queryOptions setPageSize:@20];
[queryOptions setOffset:@5];

BackendlessDataQuery *dataQuery = [BackendlessDataQuery new];
// set where clause
[dataQuery setWhereClause:@"age > 30"];
// set query options into data query
[dataQuery setQueryOptions:queryOptions];

MapDrivenDataStore *contactStorage = [backendless.data ofTable:@"Contact"];
[contactStorage find:dataQuery response:^(BackendlessCollection *contactObjects) {
    NSLog(@"Retrieved %i objects", (int)[contactObjects getCurrentPage].count);
} error:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let queryOptions = QueryOptions()
// set related columns
queryOptions.addRelated("address")
// request sorting
let sortBy = ["name"]
queryOptions.sortBy = sortBy
// set page size and offset
queryOptions.pageSize = 20
queryOptions.offset = 5

let dataQuery = BackendlessDataQuery()
// set where clause
dataQuery.whereClause = "age > 30"
// set query options into data query
dataQuery.queryOptions = queryOptions

let contactStorage = Backendless.sharedInstance()?.data.ofTable("Contact")
contactStorage?.find(dataQuery, response: { contactObjects in
    print("Retrieved \(contactObjects?.getCurrentPage()?.count ?? 0) objects")
}, error: { fault in
    print("Error: \(fault?.message ?? "")")
})

Version 5.x

DataQueryBuilder *queryBuilder = [DataQueryBuilder new];
// set where clause
[queryBuilder setWhereClauseWithWhereClause:@"age > 30"];
// set related columns
[queryBuilder addRelatedWithRelated:@"address"];
// request sorting
NSArray *sortBy = @[@"name"];
[queryBuilder setSortBySortBy:sortBy];
// set page size and offset
[queryBuilder setPageSizeWithPageSize:20];
[queryBuilder setOffsetWithOffset:5];

MapDrivenDataStore *contactStorage = [Backendless.shared.data ofTable:@"Contact"];
[contactStorage findWithQueryBuilder:queryBuilder responseHandler:^(NSArray *contactObjects) {
    NSLog(@"Retrieved %i objects", (int)contactObjects.count);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let queryBuilder = DataQueryBuilder()
// set where clause
queryBuilder.setWhereClause(whereClause: "age > 30")
// set related columns
queryBuilder.addRelated(related: "address")
// requesting sorting
let sortBy = ["name"]
queryBuilder.setSortBy(sortBy: sortBy)
// set page size and offset
queryBuilder.setPageSize(pageSize: 20)
queryBuilder.setOffset(offset: 5)

let contactStorage = Backendless.shared.data.ofTable("Contact")
contactStorage.find(queryBuilder: queryBuilder, responseHandler: { contactObjects in
    print("Retrieved \(contactObjects.count) objects")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

Additional resources: