CloudKit统计记录 [英] CloudKit count records

查看:252
本文介绍了CloudKit统计记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可能有很多记录的表,在添加新记录时我需要知道当前表中已有多少记录,因为我在计算某些值时使用它。我能找到的最接近的是请求所有这样的条目:

I have a "table" that can potentially have many records, when adding a new record I need to know how many records there already are in current table as I use it in calculation of some values. The closest thing I could find is requesting all entries like this:

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一起提供计数或结果数组,但遗憾的是它没有。

I wish queryCompletionBlock gave a count or results array along with CKQueryCursor, but unfortunately it does not.

有没有其他方法来计算表格中的行数?

Is there any other way to calculate number of rows in the table?

推荐答案

没有,这是不可能得到的,也应该符合您的查询的记录的总数。同样通过枚举结果,答案可能是错误的。 CloudKit返回的记录数量不固定。 CloudKit有一种机制来决定返回多少记录。可以在CKQueryOperation对象上将其设置为固定数字。默认值为:

No, it's not possible to get the total number of records that comply to your query. Also by enumerating the result the answer could be wrong. The number of records returned by CloudKit is not fixed. CloudKit has a mechanism for deciding how many records to return. It is possible to set this to a fixed number on the CKQueryOperation object. The default is:

operation.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.

如果您的计数与此固定数字相同,则可能会有比您的查询返回的记录更多的记录。

If your count is the same as this fixed number, then there are probably more records than your query returned.

这篇关于CloudKit统计记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆