cloudkit - 我不想下载带有 table 的图像

cloudkit - I do not want the images with my table to download

我有一个 table,它有 3 个文本字段和一个图像 (CKAsset)。图片很大。我想查询 table 并且只接收 2 个文本字段而不下载图像。有没有办法指定我只需要 2 个文本字段?

这是我正在使用的代码:

let predicate = NSPredicate(format: "CloudID = %@", tempCloudID)
    let query = CKQuery(recordType: "Images", predicate: predicate)
    query.sortDescriptors = [NSSortDescriptor(key: "Field1", ascending: true)]
    query.sortDescriptors = [NSSortDescriptor(key: "Field2", ascending: true)]

仍然无法查询 tables,它们虽然与引用一起正确存储,并且在我删除拥有的 table.

时被删除
func test() {
    //This works for storing my images, and I can delete the parent and it successfully removes all images. That’s what I want.
    let parentId = CKRecordID(recordName: uploadList[uploadRecord].CloudID)
    let parent = CKReference(recordID: parentId, action: CKReferenceAction.DeleteSelf)
    let outputPath = NSHomeDirectory().stringByAppendingString("/Library/Pictures/" + uploadList[uploadRecord].File)
    let File: CKAsset? = CKAsset(fileURL: NSURL(fileURLWithPath: outputPath))
    let imageRecord = CKRecord(recordType: "Images")
    imageRecord.setObject(uploadList[uploadRecord].R, forKey: "R")
    imageRecord.setObject(uploadList[uploadRecord].A, forKey: "A")
    imageRecord.setObject(parent, forKey: "Reference")
    imageRecord.setValue(File, forKey: "Image")
    publicDB.saveRecord(imageRecord, completionHandler: ({returnRecord, error in
        if let err = error {
            self.notifyUser("Save Image Error", message:
                err.localizedDescription)
            print("Image Upload Error")
        } else {
            dispatch_async(dispatch_get_main_queue()) {
                print("Image Sent Successfully")
                // Mark image to be deleted?
            }
        }
    }))

}

我试过通过addoperation查询并执行查询。我做错了什么,不确定是什么。

您可以通过两种方式解决此问题:

  1. 您可以使用 CKQueryOperation,您可以在其中指定所需的键。如果您确实想要资产,则必须使用资产的密钥查询/获取相同的记录。您可以使用 fetchRecordWithID 来做到这一点。这样做的缺点是它会再次获得整个记录。如果一条记录包含 2 个资产,则不能指定只获取一个。
  2. 您可以将 CKAssets 放在单独的资产 recordType 中,并为其设置 CKReference。然后您可以有选择地只获取该特定资产记录。

P.S。可以使用如下代码查询如下所示的参考:

 let parentId = CKRecordID(recordName: referenceRecordName)
 let parent = CKReference(recordID: parentId, action: CKReferenceAction.None)
 let query = CKQuery(recordType: recordType, predicate: NSPredicate(format: "%K == %@", referenceField ,parent))