4 Replies
      Latest reply on Dec 9, 2019 7:29 PM by PBK
      • Re: How do I fetch all records in a record type?
        PBK Level 7 Level 7 (3,435 points)

        >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){
                    //handle error
                    //start a recursive block, needed if you have more than about 50 records
                    CKQueryOperation *anotherOperation=[[CKQueryOperation alloc] initWithCursor:theCursor];
                    [self startAQueryOperation:anotherOperation];
                   //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];