Search code examples
iosswifticloudcloudkit

Fetching and Saving to device all records from a Public Record type


I am new to iOS development and just got started using swift. I am using Cloud Kit. I am trying to fetch and save all the records in a record type called Puppets. It is an extremely small text dataset out will need updating.

How can I fetch and save all records from one Record Type, using swift?

Here is the code I got from the Documentation:

CKDatabase *publicDatabase = [[CKContainer defaultContainer] publicCloudDatabase];
        CKRecordID *artworkRecordID = [[CKRecordID alloc] initWithRecordName:@"Puppets"];
        [publicDatabase fetchRecordWithID:artworkRecordID completionHandler:^(CKRecord *artworkRecord, NSError *error) {
        if (error) {
        // Error handling for failed fetch from public database
        }
        else {
        // Display the fetched record
        }
        }];

Solution

  • As @rmaddy already responded, if you want to query a recordType for multiple records you can use a CKQuery. Even better would be a CKQueryOperation. You will have more control over the query. You also have to be aware that the number of records returned is limited (usually to 100). If you want to read more records, then you have to start an other query where you use the cursor that is returned by the first query.

    Here you can see some sample code how EVCloudKitDao snippet