I have currently got CloudKit set up in my app so that I am adding a new record using the help of the following code below,
CKRecordID *recordID = [[CKRecordID alloc] initWithRecordName:@"stringArray"];
CKRecord *record = [[CKRecord alloc] initWithRecordType:@"Strings" recordID:recordID];
[record setObject:[NSArray arrayWithObjects:@"one", @"two", @"three", @"four", nil] forKey:@"stringArray"];
[_privateDatabase saveRecord:record completionHandler:nil];
However, now I would like to be able to fetch ALL records that are of the same record type, "Strings," and return those compiled into an NSArray. How would I go about doing that? Currently, all I have figured out is how to fetch each record individually, using a recordID, which is a hassle, there must be an easier way.
[_privateDatabase fetchRecordWithID:recordID completionHandler:^(CKRecord *record, NSError *error) {
if (error) {
// Error handling for failed fetch from private database
}
else {
NSLog(@"ICLOUD TEST: %@", [record objectForKey:@"stringArray"]);
}
}];
Aaaand, I've got it. Using the code below, I was able to create a query to run on the database, to then return an NSArray in the completion block, which I looped through, and returned the value for the saved key in an NSLog.
NSPredicate *predicate = [NSPredicate predicateWithValue:YES];
CKQuery *query = [[CKQuery alloc] initWithRecordType:@"Strings" predicate:predicate];
[_privateDatabase performQuery:query inZoneWithID:nil completionHandler:^(NSArray *results, NSError *error) {
for (CKRecord *record in results) {
NSLog(@"Contents: %@", [record objectForKey:@"stringArray"]);
}
}];