搜索 return 个 cloudkit 记录字段
Search and return single field of cloudkit records
我有 cloudKit 条格式为
的记录
KeyWords : String
Content : Asset
资产约10万。
我正在使用 SearchDisplayController 查询 public 数据库,但它 return 正在查询整个记录。我能找到的每个教程也是这样做的。
有没有办法只 return KeyWords 字段,例如以某种方式使用谓词?
假设您正在使用 CKQueryOperation 获取记录,您可以设置其 desiredKeys 属性 以过滤掉不需要的值。像这样。
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
我有 cloudKit 条格式为
的记录KeyWords : String
Content : Asset
资产约10万。
我正在使用 SearchDisplayController 查询 public 数据库,但它 return 正在查询整个记录。我能找到的每个教程也是这样做的。
有没有办法只 return KeyWords 字段,例如以某种方式使用谓词?
假设您正在使用 CKQueryOperation 获取记录,您可以设置其 desiredKeys 属性 以过滤掉不需要的值。像这样。
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