2017-02-21 60 views
0

我上傳我的資產到iCloud正在獲取公共記錄從iCloud中使用CloudKit

下面的代碼
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) 
     } 
    } 
} 

它肯定的作品,因爲我可以去到CloudKit儀表盤,和實際看到的記錄,以及下載他們附加的文件。

這裏是savedRecord的一個例子是從該函數輸出:

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>>] 

我認爲,由於這是一個公共記錄,我可以下載該另一個應用程序共享的內部相同的自定義iCloud容器字符串。

但是,使用RecordIDName(在這種情況下:F6DF33CB-35DA-483A-A5B5-33542E435689),我無法獲取記錄。

這是我下載它的代碼:

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) 
       } 
      } 
     } 
    } 

我只是得到的錯誤如下:

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

任何幫助表示讚賞。提前致謝! :)

回答

2

錯誤是使用

CKContainer.default() 

在兼具創造和拉動階段。相反,我應該在內部使用指定的定製容器,即

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