嗨,我正尝试从cloudkit中的publicDB中获取所有记录,目前有2000条记录。如何获取所有内容并将它们放入数组中? 我尝试了这两种方法都没有成功。你能帮我吗?
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Position", predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
publicDB.perform(query, inZoneWith: nil) { (results, error) -> Void in
if error != nil {
DispatchQueue.main.async(execute: { () -> Void in
self.delegate?.errorUpdating(error: error! as NSError)
return
})
} else {
self.positionArray.removeAll(keepingCapacity: true)
for record in results! {
let position = Position(record: record as CKRecord, database: self.publicDB)
self.positionArray.append(position)
}
}
DispatchQueue.main.async(execute: { () -> Void in
/* my elaboration with the complete result */
})
}
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Position", predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let qop = CKQueryOperation(query: query)
qop.resultsLimit = 3000
qop.recordFetchedBlock = { (record: CKRecord) in
let position = Position(record: record, database: self.publicDB)
self.positionArray.append(position)
print(self.positionArray.count)
}
qop.queryCompletionBlock = { (cursor: CKQueryOperation.Cursor?, error: Error?) in
DispatchQueue.main.async(execute: { () -> Void in
if let cursor = cursor {
print("entro")
let newOperation = CKQueryOperation(cursor: cursor)
newOperation.recordFetchedBlock = qop.recordFetchedBlock
newOperation.queryCompletionBlock = qop.queryCompletionBlock
self.publicDB.add(newOperation)
}
else if let error = error {
print("Error:", error)
}
// No error and no cursor means the operation was successful
else {
print("Finished with records:", self.positionArray)
if(!all){
// my elaboration
}
else{
// my elaboration
}
}
})
}
self.publicDB.add(qop)
使用第一种方法,我最多可以获取100条记录。 使用第二种方法,我最多可以获取400条记录。 但是我需要用超过2000条记录填充我的数组,如何获得结果?
答案 0 :(得分:1)
请尝试这个!
var Whistle = [CKRecord]()
func loadWhistles(completionHandler: (() -> Void)?) {
let pred = NSPredicate(value: true)
let sort = NSSortDescriptor(key: nomeDaTabela, ascending: ordemDaTabela)
let query = CKQuery(recordType: "Cliente", predicate: pred)
query.sortDescriptors = [sort]
let operation = CKQueryOperation(query: query)
operation.desiredKeys = ["Name"]
operation.resultsLimit = 2000
var newWhistles = Whistle
operation.recordFetchedBlock = { record in
newWhistles.append(record)
}
operation.queryCompletionBlock = { [unowned self] (cursor, error) in
DispatchQueue.main.async {
if error == nil {
//ViewController.isDirty = false
self.clienteRecords = newWhistles
self.clientesTableView.reloadData()
} else {
let ac = UIAlertController(title: "Fetch failed", message: "There was a problem fetching the list of whistles; please try again: \(error!.localizedDescription)", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
self.present(ac, animated: true)
}
}
}
PublicDatabase.add(operation)
completionHandler?()
}
答案 1 :(得分:0)
每个提取操作确实有400条记录的限制,因此您需要检查查询完成块返回的CKQueryCursor
值,如果不是nil
,则开始CKQueryOperation(cursor: cursor)
的另一项操作。
所以原则上您的第二种方法是正确的。我的猜测是您的问题是由于线程问题引起的,请尝试删除DispatchQueue.main.async(execute: { () -> Void in
。
P.S。看看如何在RxCloudKit
中完成操作(它会自动处理大量提取操作),它绝对可以读取1000多个记录-RecordFetcher.queryCompletionBlock(cursor: CKQueryCursor?, error: Error?)