Search code examples
iosios8icloudcloudkitckquery

Filter CKQuery by key value


My scenario: I have a list of records of a certain record type in the public database. When someone presses a button, I want to only show records that contains the value "1" in the "Example" key. Any idea how to do this while the array of CKRecords is being created? This is how I currently build the array:

- (void)queryCloudKit
{
    CKQuery *query = [[CKQuery alloc] initWithRecordType:@"NewArticle"
                                               predicate:[NSPredicate predicateWithValue:YES]];
    query.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"Date" ascending:NO]];

    [self.publicDatabase performQuery:query
                         inZoneWithID:nil
                         completionHandler:^(NSArray *results, NSError *error) {

        if (!error) {
            // CK convenience methods completion
            // arent executed on the callers thread.
            // more info: http://openradar.appspot.com/radar?id=5534800471392256
            @synchronized(self){
                self.results = [results mutableCopy];
                // make sure it executes on main thread
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.collectionView reloadData];
                });
            }
        }
        else {
            NSLog(@"FETCH ERROR: %@", error);
        }
    }];
}

Thanks!


Solution

  • You might try

    CKQuery *query = [[CKQuery alloc] initWithRecordType:@"NewArticle" predicate:[NSPredicate predicateWithFormat:@"Example == 1"]];

    NSPredicate Programming Guide