Fetching Public Record From iCloud Using CloudKit

2019-09-07 05:22发布

问题:

I have the following code for uploading my assets to iCloud

func uploadAsset(recordType: String, fileURL: URL) {
    let record = CKRecord(recordType: recordType)
    record["file"] = CKAsset(fileURL: fileURL)

    CKContainer.default().publicCloudDatabase.save(record) { savedRecord, error in
        if let saveError = error {
            print("An error occurred in \(saveError)")
        } else {
            print(savedRecord)
        }
    }
}

It definitely works because I am able to go onto the CloudKit dashboard, and physically see the records as well as download their attached files.

Here is an example of the savedRecord that is output from that function:

Optional(<CKRecord: 0x100f27f60; recordID=F6DF33CB-35DA-483A-A5B5-33542E435689:(_defaultZone:__defaultOwner__), recordChangeTag=izfmgx06, values={
    file = "<CKAsset: 0x1703e6800; path=/private/var/mobile/Containers/Shared/AppGroup/992A6367-11F5-491F-BE37-C9E4BBA865E6/voice/1487611173.95385.m4a, size=33547, UUID=D8F70489-1FF8-4CE8-99BE-390741E8136D, signature=<01a5bd3c fd9b8042 cb52cc23 3681af91 50aacaa5 97>>";
}, recordType=Voice>
{
    creatorUserRecordID -> <CKRecordID: 0x17003ebe0; recordName=__defaultOwner__, zoneID=_defaultZone:__defaultOwner__>
    lastModifiedUserRecordID -> <CKRecordID: 0x17003e740; recordName=__defaultOwner__, zoneID=_defaultZone:__defaultOwner__>
    creationDate -> 2017-02-21 14:24:28 +0000
    modificationDate -> 2017-02-21 14:24:28 +0000
    modifiedByDevice -> 3AB140FD824827C374412EBB6B72307C57DCF1E07A7FE58F13EBF445ACCB0EA3
    file -> <CKAsset: 0x1703e6800; path=/private/var/mobile/Containers/Shared/AppGroup/992A6367-11F5-491F-BE37-C9E4BBA865E6/voice/1487611173.95385.m4a, size=33547, UUID=D8F70489-1FF8-4CE8-99BE-390741E8136D, signature=<01a5bd3c fd9b8042 cb52cc23 3681af91 50aacaa5 97>>]

I thought, due to this being a public record, I'd be able to download this inside another app that shares the same custom iCloud container string.

However, using the RecordIDName (in this case: F6DF33CB-35DA-483A-A5B5-33542E435689), I am unable to fetch the record.

This is my code for downloading it:

    let recordId = CKRecordID(recordName: "F6DF33CB-35DA-483A-A5B5-33542E435689")


    CKContainer.default().publicCloudDatabase.fetch(withRecordID: recordId) { (results, error) -> Void in

        DispatchQueue.main.async {
            if error != nil {

                print(" Error Fetching Record  " + error!.localizedDescription)
            } else {
                if results != nil {
                    print("pulled record")

                    let record = results as CKRecord!

                    print(record)
                }
            }
        }
    }

I simply get the error as follows:

 Error Fetching Record  Error fetching record <CKRecordID: 0x10024c6f0; recordName=6988103A-E795-441B-A115-2D044872AAAC, zoneID=_defaultZone:__defaultOwner__> from server: Record not found

Any help appreciated. Thanks in advance! :)

回答1:

The error was in using

CKContainer.default()

In both the creation and pulling stages. Instead, I should have used a specified custom container inside, i.e.

CKContainer(identifier: "iCloud.tech.jackrobson.myApp")