我有一个“表”可能有许多记录,当添加一个新记录时,我需要知道当前表中已经有多少条记录,因为我在计算某些值时使用它.我找到的最接近的事情是请求所有这样的条目:
var query : CKQuery = CKQuery(recordType: "Stars",predicate: NSPredicate(format: "mass > 0")) var request : CKQueryOperation = CKQueryOperation(query: query) var starCount = 0 request.queryCompletionBlock = { (cursor:CKQueryCursor!,error:NSError!) in if error { completionHandler(ECOResponse.error(error.description),starCount) } else { completionHandler(ECOResponse.ok(),starCount) } } request.recordFetchedBlock = { (record:CKRecord!) in starCount += 1 }
我希望queryCompletionBlock给出一个计数或结果数组以及CKQueryCursor,但不幸的是它没有.
有没有办法计算表中的行数?
不,不可能获得符合您的查询的记录总数.也可以列举结果,答案可能是错误的. CloudKit返回的记录数并不固定. CloudKit有一个机制来决定要返回多少条记录.可以在CKQueryOperation对象上将其设置为固定数字.默认值为:
原文链接:https://www.f2er.com/swift/319697.htmloperation.resultsLimit = CKQueryOperationMaximumResults;
该物业的文件说:
When using that value,the server chooses a limit that aims to provide an optimal number of results that returns as many records as possible while minimizing delays in receiving those records. However,if you know that you want to process a fixed number of results,change the value of this property accordingly.