CKQueryOperation queryCompletionBlock只运行3次

时间:2015-07-19 16:07:17

标签: macos swift cloudkit

我正在尝试使用CloudKit和光标从我的iCloud公共数据库下载一批记录。无论resultLimit如何设置,代码都可以在前3次执行中正常工作,但是从不执行第4个完成块。如果没有设置resultsLimit,我会得到300条记录,如果它设置为50则我得到150条,如果它设置为5则得到15条...

没有报告错误,并且该块似乎已添加,但从未执行过。平台是OS X.这是有问题的代码:

        let queryOp = CKQueryOperation(query: query)
        queryOp.recordFetchedBlock = self.fetchedDetailRecord
        queryOp.resultsLimit = 5
        queryOp.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 {
              let nextOp = CKQueryOperation(cursor: cursor)
              nextOp.recordFetchedBlock = self!.fetchedDetailRecord
              nextOp.queryCompletionBlock = queryOp.queryCompletionBlock
              nextOp.resultsLimit = 5
              self!.publicDatabase?.addOperation(nextOp)
              println("added next fetch")
            } else {
              self!.fileHandle!.closeFile()
              self!.exportProgressIndicator.stopAnimation(self)
            }
          }
        }

        self.publicDatabase?.addOperation(queryOp)

这是控制台的结果 -

459013587628.012 0 459013587628.621 1 459013587628.863 2 459013587629.113 3 459013587629.339 4 使用nil调用comp块 添加了下一个提取 459013587828.552 5 459013587828.954 6 459013587829.198 7 459013587829.421 8 459013587829.611 9 使用nil调用comp块 添加了下一个提取 459013587997.084 10 459013587997.479 11 459013587997.74 12 459013587997.98 13 459013587998.207 14

大数字只是对recordFetchedBlock的调用之间的时间,第二个数字是调用该块的次数。

我很难过...有关如何进行的任何想法?哦,container和publicDatabase是类变量,并在运行上面的代码片段之前进行了初始化。

2 个答案:

答案 0 :(得分:6)

您在queryCompletionBlock范围内定义nextOp的方式在该块的三次迭代后导致问题。如果你将CKQueryOperation的创建分解为自己的方法,你的问题就会消失。

以下是代码:

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

答案 1 :(得分:4)

这可能是另一种方法(已经更新到Swift 3)。

它在第三次迭代后不会停止,而是一直持续到结束

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)

我成功地使用它从CloudKit中检索了数百条记录