CloudKit CompletionBlock上对dispatch_group_leave()的不平衡调用

时间:2015-06-20 03:32:12

标签: ios asynchronous grand-central-dispatch cloudkit dispatch-async

所以我正在构建一个使用CloudKit的iOS应用程序。特定功能需要产品:

func getAllProducts(){

    //empty object in Singleton Class
    sharedStore.products.removeAllObjects()
    var products = NSMutableArray()

    //set up a query
    var predicate = NSPredicate(value: true)
    var query = CKQuery(recordType: "Product", predicate: predicate)

    //set in query operation
    var queryOperation = CKQueryOperation(query: query)

    //define method to catch fetched records
    //define completion block to process results
    queryOperation.queryCompletionBlock = { (cursor: CKQueryCursor!, error: NSError!) in
        if error != nil {
            //there was an error, get this error to the delegate
            self.delegate?.cloudKitReturnedError(error)
        } else {
            //there was no error
            println("no error")
            //cursor is used to know if there is more data to be fetched, so check cursor
            if cursor != nil {
                //there is more data to fetch
                println("more data to be fetched")
                //start new query
                let newOperation = CKQueryOperation(cursor: cursor)
                newOperation.completionBlock = queryOperation.completionBlock
                newOperation.recordFetchedBlock = queryOperation.recordFetchedBlock
                self.privateDB.addOperation(newOperation)
                dispatch_async(dispatch_group_enter(<#group: dispatch_group_t#>), <#block: dispatch_block_t##() -> Void#>)
            } else {
                println("all data fetched")
                self.delegate?.cloudKitReturnedArray!(products, identifier: "StoreProducts")
            }
        }
    }

    queryOperation.recordFetchedBlock = { (record: CKRecord!) in
        let product = self.saveProduct(record)

        if product.categoryReference == nil {
            products.addObject(product)
        }
    }

    //add the operation
    self.privateDB .addOperation(queryOperation)
}

请注意,所有CloudKit通信都在专用的CloudKitController类中进行。

当要获取大量数据时,它不会立即加载它们,而是“激活”它的光标。但是,当它这样做时,它显然在后台某处调用dispatch_group_leave()。当它的光标开始接收数据并完成时,我的queryCompletionBlock应该向它的委托发送一条“已完成”的消息,但我得到了这个:

libdispatch.dylib`dispatch_group_leave:
0x1088edfd9 <+0>:  movl   $0x1, %eax
0x1088edfde <+5>:  lock   
0x1088edfdf <+6>:  xaddq  %rax, 0x40(%rdi)
0x1088edfe4 <+11>: incq   %rax
0x1088edfe7 <+14>: js     0x1088edffe               ; <+37>
0x1088edfe9 <+16>: movabsq $0x7fffffffffffffff, %rcx
0x1088edff3 <+26>: cmpq   %rcx, %rax
0x1088edff6 <+29>: jne    0x1088edffd               ; <+36>
0x1088edff8 <+31>: jmp    0x1088ee00e               ; _dispatch_group_wake
0x1088edffd <+36>: retq   
0x1088edffe <+37>: leaq   0x180c8(%rip), %rcx       ; "BUG IN CLIENT OF LIBDISPATCH: Unbalanced call to dispatch_group_leave()"
0x1088ee005 <+44>: movq   %rcx, 0x301a4(%rip)       ; gCRAnnotations + 8

-> 0x1088ee00c <+51>: ud2

我想我应该在我的代码中以某种方式调用dispatch_group_enter(),但这整个异步对我来说都是新的。我已阅读Apple的文档,但我无法编写一段代码来完成这项工作。

问题 当CloudKit使用游标来检索大量数据时,如何阻止将此非平衡调用发送到“dispatch_group_leave()”?

任何评论都非常感谢。

0 个答案:

没有答案