Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

ios - CloudKit count records

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
    }

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

The documentations for this property says:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...