CKQueryOperation queryCompletionBlock仅运行3次 [英] CKQueryOperation queryCompletionBlock only runs 3 times

查看:80
本文介绍了CKQueryOperation queryCompletionBlock仅运行3次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用CloudKit和游标从我的iCloud公共数据库中下载一批记录.不管resultLimit的设置方式如何,该代码对于前3次执行均能正常工作,但永远不会执行第4个完成代码块.如果没有设置resultsLimit,我将得到300条记录;如果将它设置为50,我将得到150条记录;如果将其设置为5,我将得到15条记录...

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 the resultsLimit is set, but the 4th completion block is never executed. If resultsLimit is not set I get 300 records, if it's set to 50 I get 150, if it's set to 5 I get 15...

未报告任何错误,并且该块似乎已添加,但从未执行.平台是OSX.这是有问题的代码:

No error is reported and the block appears to be added but is never executed. Platform is OS X. Here's the code in question:

        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

459013587628.012 0 459013587628.621 1 459013587628.863 2 459013587629.113 3 459013587629.339 4 comp block called with nil added next fetch 459013587828.552 5 459013587828.954 6 459013587829.198 7 459013587829.421 8 459013587829.611 9 comp block called with nil added next fetch 459013587997.084 10 459013587997.479 11 459013587997.74 12 459013587997.98 13 459013587998.207 14

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

The big number is just the time between calls to the recordFetchedBlock with the second number being the count of times that block has been called.

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

I'm stumped...any ideas on how to proceed? Oh, container and publicDatabase are class variables and initialized prior to running the code snippet above.

推荐答案

在queryCompletionBlock范围内定义nextOp的方式在该块进行了三次迭代后引起了问题.如果您将CKQueryOperation的创建分解为自己的方法,那么您的问题就消失了.

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.

这是代码:

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

这篇关于CKQueryOperation queryCompletionBlock仅运行3次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆