ios - Swift - 从 Cloudkit 公共数据库中获取所有记录
问题描述
嗨,我正在尝试从 cloudkit 中的 publicDB 中获取所有记录,目前有超过 2000 条记录。我怎样才能将它们全部取出并将它们放入一个数组中?我试过这两种方法都没有成功。你能帮我么?
1 方法
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 */
})
}
2 方法
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 条记录填充我的数组,如何才能达到结果?
解决方案
请,试试这个!
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?()
}
推荐阅读
- sql - 是否可以通过签名/哈希码等方式访问记录
- linux - 在 Qemu 上运行的 RISC-V Linux
- mysql - 如何在 vb.net 中从 mysql 加载超过 2 万条数据到数据表,而不会在数据读取过程中遇到致命错误
- python - 从 JSON 创建字典
- excel - 数据连接刷新后保存 PDF
- jetty - 一旦我将 keycloak 适配器应用于我的 jetty_base,Jetty 9.3 和 9.4 就无法启动
- c# - IIS 和 Windows 服务中托管的 WCF 服务的 OpenTelemetry
- python - Python:汇总 CSV 中的值
- python - Python - 根据给定的键数动态创建新字典中的键数:值:来自另一个字典的值
- mysql - mysql有条件地在where子句中应用乘法因子