2 Replies
      Latest reply on Jul 27, 2019 2:42 PM by PBK
      • Re: How do I fetch all records in a record type?
        PBK Level 7 Level 7 (3,195 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){
                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];
        }
            
            
        
          • Re: How do I fetch all records in a record type?
            PBK Level 7 Level 7 (3,195 points)

            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.