对Cloudkit私有默认区域的查询结果是否有任何限制?我不知道为什么我只收到带有以下查询的前 100 条记录:
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)
}
好的。正如 Edwin 在回答中提到的那样,解决方案是使用 CKQueryOperation 来获取初始数据块,然后使用 completionBlock 中的“光标”来触发另一个操作。这是一个例子:
更新
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)")
}