iOS 6 中完成块的 dispatch_get_current_queue() 的替代方法? [英] Alternatives to dispatch_get_current_queue() for completion blocks in iOS 6?
问题描述
我有一个接受块和完成块的方法.第一个块应该在后台运行,而完成块应该在调用方法的任何队列中运行.
I have a method that accepts a block and a completion block. The first block should run in the background, while the completion block should run in whatever queue the method was called.
对于后者,我总是使用 dispatch_get_current_queue()
,但它似乎在 iOS 6 或更高版本中已弃用.我应该用什么代替?
For the latter I always used dispatch_get_current_queue()
, but it seems like it's deprecated in iOS 6 or higher. What should I use instead?
推荐答案
在调用者所在的任何队列上运行"的模式很吸引人,但最终不是一个好主意.该队列可以是低优先级队列、主队列或其他具有奇怪属性的队列.
The pattern of "run on whatever queue the caller was on" is appealing, but ultimately not a great idea. That queue could be a low priority queue, the main queue, or some other queue with odd properties.
我最喜欢的方法是说完成块在具有以下属性的实现定义的队列上运行:x、y、z",如果调用者想要更多的控制,让块分派到特定队列.要指定的一组典型属性类似于与任何其他应用程序可见的队列相关的串行、不可重入和异步".
My favorite approach to this is to say "the completion block runs on an implementation defined queue with these properties: x, y, z", and let the block dispatch to a particular queue if the caller wants more control than that. A typical set of properties to specify would be something like "serial, non-reentrant, and async with respect to any other application-visible queue".
** 编辑 **
Catfish_Man 在下面的评论中举了一个例子,我只是将它添加到他的答案中.
Catfish_Man put an example in the comments below, I'm just adding it to his answer.
- (void) aMethodWithCompletionBlock:(dispatch_block_t)completionHandler
{
dispatch_async(self.workQueue, ^{
[self doSomeWork];
dispatch_async(self.callbackQueue, completionHandler);
}
}
这篇关于iOS 6 中完成块的 dispatch_get_current_queue() 的替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!