iOS 6中完成块的dispatch_get_current_queue()的替代方法? [英] Alternatives to dispatch_get_current_queue() for completion blocks in iOS 6?

查看:1299
本文介绍了iOS 6中完成块的dispatch_get_current_queue()的替代方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法接受一个块和一个完成块。第一个块应该在后台运行,而完成块应该在调用该方法的任何队列中运行。

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".

** EDIT * *

** EDIT **

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屋!

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