CKQueryOperation queryCompletionBlock only runs 3 times

前端 未结 2 1498
广开言路
广开言路 2021-01-17 21:41

I\'m trying to download a batch of records from my iCloud public database using CloudKit and the cursor. The code works fine for the first 3 executions, regardless of how t

2条回答
  •  情深已故
    2021-01-17 22:06

    This could be another approach (already updated to Swift 3).

    It does not stops after third iteration, but continues until the end

    var queryOp = CKQueryOperation(query: query)
    queryOp.recordFetchedBlock = self.fetchedARecord
    queryOp.resultsLimit = 5
    queryOp.queryCompletionBlock = { [weak self] (cursor : CKQueryCursor?, error : Error?) -> Void in
        print("comp block called with \(cursor) \(error)")
    
        if cursor != nil {
            let nextOp = CKQueryOperation(cursor: cursor!)
            nextOp.recordFetchedBlock = self!.fetchedARecord
            nextOp.queryCompletionBlock = queryOp.queryCompletionBlock
            nextOp.resultsLimit = queryOp.resultsLimit
    
            //this is VERY important
            queryOp = nextOp
    
            self!.publicDB.add(queryOp)
            print("added next fetch")
        }
    
    }
    
    self.publicDB.add(queryOp)
    

    I successfully use it to retrieve hundreds of records from CloudKit

提交回复
热议问题