I want to fetch all records in a record type and then add them all to a picker view. Each record has a few indexes which i would like to access.
How do I fetch all records in a record type?
>I want to fetch all records in a record type and then add them all to a picker view
//create a global array:
NSMutableArray *theRecords=[[NSMutableArray alloc] init];
-(void fetchAllRecordsWithRecordType:(NSString *)recordType
NSPredicate *predicate =[NSPredicate predicateWithValue:YES];
CKRecordType *theRecordType=recordType;
//I am a bit confused here about the "*" above, I have just CKRecordType recordType=@"Notices"
CKQuery *query = [[CKQuery alloc] initWithRecordType:recordType predicate:predicate];
CKQueryOperation *theOperation=[[CKQueryOperation alloc] initWithQuery:query];
[self startAQueryOperation:theOperation];
}
-(void)startAQueryOperation:(CKQueryOperation *)theOperation{
theOperation.recordFetchedBlock=^(CKRecord *theRecord){
[theRecords addObject:theRecord];
};
theOperation.queryCompletionBlock=^(CKQueryCursor *theCursor, NSError *error){
if(error){
//handle error
}
if(theCursor){
//start a recursive block, needed if you have more than about 50 records
CKQueryOperation *anotherOperation=[[CKQueryOperation alloc] initWithCursor:theCursor];
[self startAQueryOperation:anotherOperation];
}else{
//all done, process theRecords
dispatch_async(dispatch_get_main_queue(), ^{
[myPickerView reloadAllComponents]
// and in the delegate of myPickerView refer to theRecords
});
}
};
CKDatabase *publicDatabase = [[CKContainer defaultContainer] publicCloudDatabase];
[publicDatabase addOperation:theOperation];
}
I just realized that the above is a bit more complicated then it needs to be. You can replace the line:
[self startAQueryOperation:anotherOperation];
with:
[[[CKContainer defaultContainer] publicCloudDatabase] addOperation:anotherOperation];
since anotherOperation already contains the queryCompletionBlock and recordFetchedBlock.
Your code here is not helpful you dont explain what is anotherOperation as it does not appear in your code
See line 24 in the code.