CKQueryOperation queryCompletionBlock only runs 3 times

自作多情 提交于 2019-12-01 16:13:58

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!