将CKRecordID保存到CloudKit

时间:2016-07-19 15:42:32

标签: ios swift cloudkit ckrecord ckreference

我需要将CKRecordID保存到CloudKit作为参考。但是,当我尝试保存它时,我收到以下错误:

  

无法转换类型' CKRecordID的价值?'预期参数类型' CKRecordValue?'

currentSelectedUser声明:

var currentlySelectedUser: CKRecordID?

在另一个CKQuery中检索currentSelectedUser:

self.currentlySelectedUser = record.creatorUserRecordID

保存currentSelectedUser:

myRecord.setObject(currentlySelectedUser, forKey: "toUser")

错误发生在第3行代码中。

如何将此CKRecordID保存为另一个CloudKit记录中的参考?

1 个答案:

答案 0 :(得分:2)

您正在尝试添加CKRecordID,而CKRecord不是CKRecordValue支持的数据类型。正如错误所述,它不符合CKReference协议。

创建myRecord["toUser"] = CKReference(recordID: currentlySelectedUser!, action: .None) 并按如下方式添加:

$scope.upload = function(sourceType) {

 var options = {
        quality : 75,
        destinationType : Camera.DestinationType.FILE_URI,
        sourceType : sourceType,
        encodingType: Camera.EncodingType.JPEG,
        popoverOptions: CameraPopoverOptions,
        targetWidth: 500,
        targetHeight: 500,
        saveToPhotoAlbum: false
    };
    $cordovaCamera.getPicture(options).then(function(imageData) {

 // $scope.images = imageData;

 var storageRef = firebase.storage().ref();
 // filename = imageData.name;

var getFileBlob = function(url, cb) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url);
    xhr.responseType = "blob";
    xhr.addEventListener('load', function() {
        cb(xhr.response);
    });
    xhr.send();
};

var blobToFile = function(blob, name) {
    blob.lastModifiedDate = new Date();
    blob.name = name;
    return blob;
};

var getFileObject = function(filePathOrUrl, cb) {
    getFileBlob(filePathOrUrl, function(blob) {
        cb(blobToFile(blob, new Date().getTime()));
    });
};

getFileObject(imageData, function(fileObject) {

    var metadata = {
    'contentType': fileObject.type
  };
    storageRef.child('images/' + fileObject.name).put(fileObject, metadata);

    uploadTask.on('state_changed', null, function(error) {
    // [START onfailure]
    console.error('Upload failed:', error);
    alert('Upload failed:', error);
    // [END onfailure]
  }, function() {
    console.log('Uploaded',uploadTask.snapshot.totalBytes,'bytes.');
    console.log(uploadTask.snapshot.metadata);
    var url = uploadTask.snapshot.metadata.downloadURLs[0];
    console.log('File available at', url);
    // [START_EXCLUDE]
    document.getElementById('linkbox').innerHTML = '<img src="' +  url + '">';
    // [END_EXCLUDE]
  });

});
    }, function(error) {
        console.error(error);
        alert(error);
    });


};