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

时间:2015-07-14 09:34:32

标签: ios swift cloudkit

我的格式为

的cloudKit记录
KeyWords : String
Content : Asset

资产约为100k。

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

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

2 个答案:

答案 0 :(得分:4)

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

fetchOperation.desiredKeys = ["KeyWords"]

答案 1 :(得分:2)

确定有用的东西。这可能是hacky,但是我在相同的情况下为其他任何人提供信息......

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