Ios 搜索并返回cloudkit记录的单个字段

Ios 搜索并返回cloudkit记录的单个字段,ios,swift,cloudkit,Ios,Swift,Cloudkit,我有cloudKit格式的记录 KeyWords : String Content : Asset 其中资产约为10万 我使用SearchDisplayController查询公共数据库,但它返回整个记录。我能找到的每一篇教程都是这样做的 有没有办法只返回关键字字段,例如以某种方式使用谓词?假设您使用CKQueryOperation获取记录,您可以将其属性设置为过滤掉不需要的值。像这样的 fetchOperation.desiredKeys = ["KeyWords"] 假设您使用CKQue

我有cloudKit格式的记录

KeyWords : String
Content : Asset
其中资产约为10万

我使用SearchDisplayController查询公共数据库,但它返回整个记录。我能找到的每一篇教程都是这样做的


有没有办法只返回关键字字段,例如以某种方式使用谓词?

假设您使用CKQueryOperation获取记录,您可以将其属性设置为过滤掉不需要的值。像这样的

fetchOperation.desiredKeys = ["KeyWords"]

假设您使用CKQueryOperation获取记录,您可以设置其属性以过滤掉不需要的值。像这样的

fetchOperation.desiredKeys = ["KeyWords"]

好的,我有办法了。这很可能是骇客,但我把它的信息为其他任何人在相同的情况下

let predicate = NSPredicate(value: true) // returns all - replace with whatever condition you want
let query = CKQuery(recordType: "Library", predicate: predicate) // create a query using the predicate
var operation = CKQueryOperation(query: query) // create an operation using the query
operation.desiredKeys = ["KeyWords"] // Array of whatever 'columns' you want to return
// operation.resultsLimit = 15 // optional limit on records

// Define a closure for what to do for each returned record
operation.recordFetchedBlock = { [weak self] (record:CKRecord!) in

    // Do whatever you want with each returned record  
    println(record.objectForKey("KeyWords"))

}

// Define a closure for when the operation is complete
operation.queryCompletionBlock = { [weak self] (cursor:CKQueryCursor!, error:NSError!) in

        if cursor != nil {
            // returns the point where the operation left off if you want t retrieve the rest of the records
        }

        dispatch_async(dispatch_get_main_queue(), { () -> Void in

            // Do what you want when process complete
            self!.tableView.reloadData()
            if error != nil {
                println("there was an error")
            }
        })
    }

self.publicDatabase!.addOperation(operation) // Perform the operation on given database

好的,我有办法了。这很可能是骇客,但我把它的信息为其他任何人在相同的情况下

let predicate = NSPredicate(value: true) // returns all - replace with whatever condition you want
let query = CKQuery(recordType: "Library", predicate: predicate) // create a query using the predicate
var operation = CKQueryOperation(query: query) // create an operation using the query
operation.desiredKeys = ["KeyWords"] // Array of whatever 'columns' you want to return
// operation.resultsLimit = 15 // optional limit on records

// Define a closure for what to do for each returned record
operation.recordFetchedBlock = { [weak self] (record:CKRecord!) in

    // Do whatever you want with each returned record  
    println(record.objectForKey("KeyWords"))

}

// Define a closure for when the operation is complete
operation.queryCompletionBlock = { [weak self] (cursor:CKQueryCursor!, error:NSError!) in

        if cursor != nil {
            // returns the point where the operation left off if you want t retrieve the rest of the records
        }

        dispatch_async(dispatch_get_main_queue(), { () -> Void in

            // Do what you want when process complete
            self!.tableView.reloadData()
            if error != nil {
                println("there was an error")
            }
        })
    }

self.publicDatabase!.addOperation(operation) // Perform the operation on given database

我使用的是
CKQuery
,我使用的是
publicDatabase?.performQuery
。是否有用于CKFetchRecordsOperation的并行方法。我发现这些文档相当迟钝@德里克:不,不幸的是没有。您必须重新编写代码才能使用具有此属性的CloudKit的NSOperation子类之一。谢谢您的帮助。我得到了一些工作,并张贴了代码作为答案。如果您不介意将目光投向它,我们将不胜感激。我使用的是
CKQuery
,我使用的是
publicDatabase?.performQuery
。是否有用于CKFetchRecordsOperation的并行方法。我发现这些文档相当迟钝@德里克:不,不幸的是没有。您必须重新编写代码才能使用具有此属性的CloudKit的NSOperation子类之一。谢谢您的帮助。我得到了一些工作,并张贴了代码作为答案。如果你不介意看一眼的话,我将不胜感激。