CKQueryOperation queryCompletionBlock only runs 3 times

前端 未结 2 1500
广开言路
广开言路 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:25

    The way you're defining nextOp inside of your queryCompletionBlock scope is causing a problem after three iterations of that block. If you break the creation of the CKQueryOperation out into its own method your problem goes away.

    Here is the code:

    func fetchedDetailRecord(record: CKRecord!) -> Void {
        println("Retrieved record: \(record)")
    }
    
    func createQueryOperation(cursor: CKQueryCursor? = nil) -> CKQueryOperation {
        var operation:CKQueryOperation
        if (cursor != nil) {
            operation = CKQueryOperation(cursor: cursor!)
        } else {
            operation = CKQueryOperation(query: CKQuery(...))
        }
        operation.recordFetchedBlock = self.fetchedDetailRecord
        operation.resultsLimit = 5
        operation.queryCompletionBlock = { [weak self]
            (cursor: CKQueryCursor!, error: NSError!) -> Void in
            println("comp block called with \(cursor) \(error)")
            if error != nil {
                println("Error on fetch \(error.userInfo)")
            } else {
                if cursor != nil {
                    self!.publicDatabase?.addOperation(self!.createQueryOperation(cursor: cursor))
                    println("added next fetch")
                } else {
                    self!.fileHandle!.closeFile()
                    self!.exportProgressIndicator.stopAnimation(self) 
                }
            }
        }
        return operation
    }
    
    func doQuery() {
        println("Querying records...")
        self.publicDatabase?.addOperation(createQueryOperation())
    }
    

提交回复
热议问题