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
456 views
in Technique[技术] by (71.8m points)

ios - CKQuery from private zone returns only first 100 CKRecords from in CloudKit

Is there any limit to the result of a query to Cloudkit private default zone? I have no clue why I only receive first 100 records with the following query:

let p = NSPredicate(format: "(type == 'entered') AND (timestamp >= %@) AND (timestamp <= %@)", from, to)
let q = CKQuery(recordType: self.beaconRecordType, predicate: p)
q.sortDescriptors = [NSSortDescriptor(key: "timestamp", ascending: true)]
self.privateDatabase?.performQuery(q, inZoneWithID: nil, completionHandler: { results, error in

    //count = 100
    println(results.count)

}

Okay. As Edwin mention in the answer, the solution is to use CKQueryOperation to fetch the initial block of data then use the "cursor" in completionBlock to fire another operation. Here is an example:

UPDATE

func fetchBeacons(from:NSDate, to:NSDate) {

    let p = NSPredicate(value: true)
    let q = CKQuery(recordType: self.beaconRecordType, predicate: p)

    let queryOperation = CKQueryOperation(query: q)

    queryOperation.recordFetchedBlock = fetchedARecord

    queryOperation.queryCompletionBlock = { [weak self] (cursor : CKQueryCursor!, error : NSError!) in

        if cursor != nil {
            println("there is more data to fetch")
            let newOperation = CKQueryOperation(cursor: cursor)
            newOperation.recordFetchedBlock = self!.fetchedARecord
            newOperation.queryCompletionBlock = queryOperation.queryCompletionBlock
            self!.privateDatabase?.addOperation(newOperation)
        }

    }

    privateDatabase?.addOperation(queryOperation)
}

var i = 0
func fetchedARecord (record: CKRecord!) {
    println("(NSDate().timeIntervalSinceReferenceDate*1000) (++i)")
}
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

100 is the default limit for standard queries. That amount is not fixed. It can vary depending on the total iCloud load. If you want to influence that amount, then you need to use CKQueryOperation and set the resultsLimit like this: operation.resultsLimit = CKQueryOperationMaximumResults; That CKQueryOperationMaximumResults is the default and will limit it to 100 (most of the time). Don't set that value too high. If you want more records, then use the cursor of the queryCompletionBlock to continue reading more records.


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

...