Skip to end of metadata
Go to start of metadata

iOS SDK Querying Content - CM


The PWCME SDK provides convenient methods that allow you to very easily search and filter for CME content. The primary classes for querying are PWCMEQuery and PWCMEQueryOperation. You can also leverage the convenience method on PWCMEContainer to perform queries.

Constructing a Query

Constructing a query takes just a few steps.

1. Create an NSPredicate.

NSComparisonPredicate *predicate1 = [NSComparisonPredicate predicateWithFormat:@"title LIKE 'engineer'"];
NSComparisonPredicate *predicate2 = [NSComparisonPredicate predicateWithFormat:@"location IN { 'Austin' }"];
NSCompoundPredicate *compound = [[NSCompoundPredicate alloc] initWithType:NSOrPredicateType 
                                subpredicates:@[predicate1, predicate2]];

2. Initialize your PWCMEQuery object with the predicate.

PWCMEQuery *query = [[PWCMEQuery alloc] initWithFieldName:@"people" predicate:compound]; 

3. Customize your query parameters (such as sorting and desired keys).

query.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"createdAt" ascending:YES]];
query.desiredKeys = @[@"firstName", @"lastName"]; 

Executing a Query

There are two ways of executing a query.

Execute a query against a PWCMEContainer:

[[PWCMEContainer defaultContainer] performQuery:query 
			      completionHandler:^(NSArray *records, NSError *error) {
    if (!error) {
        // Process the records.
    else {
        // Failed to delete a record; handle the error.
        NSLog(@"Error: %@", error.debugDescription);
Do not use this method when the number of returned records is potentially more than 100 records; for efficiency, all queries automatically limit the number of returned records based on current conditions. If your query hits the maximum value, this method returns only the first portion of the overall results. The number of returned records should be sufficient in most cases, but to get the entire set of records you must use a PWCMEQueryOperation object instead. Query operations also return a maximum number of results, but when they do, they provide a cursor object that you can use to fetch the next batch of results.

Execute a query using PWCMEQueryOperation:

PWCMEQueryOperation *operation = [[PWCMEQueryOperation alloc] initWithQuery:query];
[operation setQueryCompletionBlock:^(NSArray *records, PWCMEQueryCursor *cursor, NSError *error) {
    // Handle response.

[[PWCMEContainer defaultContainer] addOperation:operation]; 

Paging Query Responses

The results of your query may be larger than your current resultsLimit. If this is the case, a PWCMEQueryCursor object will be returned. You can use the cursor to initialize another PWCMEQueryOperation to fetch additional records:

PWCMEQueryOperation *operation = [[PWCMEQueryOperation alloc] initWithCursor:cursor];
[operation setQueryCompletionBlock:^(NSArray *records, PWCMEQueryCursor *cursor, NSError *error) {
    // Handle response

[[PWCMEContainer defaultContainer] addOperation:operation];


  • No labels