小编典典

来自私有区域的CKQuery仅返回CloudKit中的前100个CKRecords

swift

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来获取数据的初始块,然后在completeBlock中使用“游标”来触发另一个操作。这是一个例子:

更新

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)")
}

阅读 455

收藏
2020-07-07

共1个答案

小编典典

标准查询的默认限制为100。该金额不是固定的。它可能会因iCloud的总负载而异。如果要影响该金额,则需要使用CKQueryOperation并按以下方式设置resultLimit:operation.resultsLimit
= CKQueryOperationMaximumResults;
该CKQueryOperationMaximumResults是默认值,它将限制为100(大多数情况下)。不要将该值设置得太高。如果需要更多记录,请使用queryCompletionBlock的光标继续读取更多记录。

2020-07-07