GDC

来源:互联网 发布:mac卸载自带软件 编辑:程序博客网 时间:2024/05/15 23:10
创建和管理队列
dispatch_get_main_queue
返回一个和程序主线程相关的连续对联
dispatch_queue_t dispatch_get_main_queue ( void );
返回一个主队列,在主程序响应前,这个自动创建的队列代表主线程
主队列被系统自动创建,并且和你程序的主线程相关。你的应用使用下面三个方法中的一个(仅仅一个)来调用代码块提交到主队列
1.唤醒 dispatch_main
2.唤醒 UIApplicationMain (iOS)或  NSApplicationMain (OS X)
3.在主线程上使用CFRunLoopRef 
用全局的连续队列来唤醒dispatch_suspenddispatch_resumedispatch_set_context,并且没有影响使用的队列被功能返回。

dispatch_get_global_queue
返回拥有指定的服务质量的系统定义的全局并发队列

dispatch_queue_t dispatch_get_global_queue ( long identifier, unsigned long flags )
identifier:你想要使用队列执行队列,服务质量帮助决定被队列执行的任务的优先级。你能指定值QOS_CLASS_USER_INTERACTIVEQOS_CLASS_USER_INITIATEDQOS_CLASS_UTILITY,
QOS_CLASS_BACKGROUND。队列来挂起用户交互或用户发起的任务比在后台打算执行的任务有更高的优先级。
你也可以指定一个队列的优先级值,在dispatch_queue_priority_t里面找,这些值被标记在一个恰当的服务质量类
flags:为将来使用而保留,默认0

返回值:请求的全局并发队列

这个方法返回一个队列和指定的服务质量等级的正在执行的任务相适应。唤起dispatch_suspenddispatch_resume 和 dispatch_set_context 功能不影响返回队列

dispatch_queue_create
创建一个代码块能被提交的新的调度队列
dispatch_queue_t dispatch_queue_create ( const char *label, dispatch_queue_attr_t attr )
label:label字符串附加到队列来特殊的定义,它在排除故障的工具例如:nstruments, sample, stackshots, 和 crash reports 。因为应用库和框架都能创建他们自己的调度队列,一个反DNS的命名方式被推荐。这个参数是可选项可以为0
attr:在OSX v10.7之后或iOS4.3之后,指定DISPATCH_QUEUE_SERIAL (或 NULL)来创建连续的队列或指定DISPATCH_QUEUE_CONCURRENT 来创建一个并发的队列。在较早的版本这个参数必须指定为空
返回值:最近的调度队列
在FIFO命令中代码块一次提交一个连续的被执行的队列。注意,但是这个代码块提交一个独立的可能被执行的并发彼此互斥的队列。在FIFO中,代码块提交一个并发的队列被移除。但是也可能并发的运行如果资源能够这样做。
当你的应用不再需要调度队列,它能够释放它用队列释放功能。任何决定的代码块提交的队列,保留一个对此队列的参考,所以这个队列被解除配置知道所有的代码块已经被完成。

dispatch_queue_t dispatch_get_current_queue ( void )
返回当前的队列
这个方法永远不会返回null
当被外部的被提交的代码块内容所响应的时候,这个功能返回主队列,如果响应从主线程被执行。如果这个响应被任何其他的线程处理,这个方法返回默认的并发队列。

see also

Creating and Managing Queues

  • dispatch_get_main_queue

    Returns the serial dispatch queue associated with the application’s main thread.

    Declaration

    SWIFT

    func dispatch_get_main_queue() -> dispatch_queue_t!

    OBJECTIVE-C

    dispatch_queue_t dispatch_get_main_queue ( void );

    Return Value

    Returns the main queue. This queue is created automatically on behalf of the main thread before main is called.

    Discussion

    The main queue is automatically created by the system and associated with your application’s main thread. Your application uses one (and only one) of the following three approaches to invoke blocks submitted to the main queue:

    • Calling dispatch_main

    • Calling UIApplicationMain (iOS) or NSApplicationMain (OS X) 

    • Using a CFRunLoopRef on the main thread

    As with the global concurrent queues, calls to dispatch_suspenddispatch_resumedispatch_set_context, and the like have no effect when used with queues returned by this function.

    Import Statement

    import Dispatch

    Availability

    Available in iOS 4.0 and later

  • dispatch_get_global_queue

    Returns a system-defined global concurrent queue with the specified quality of service class. 

    Declaration

    SWIFT

    func dispatch_get_global_queue(_ identifierInt,
                                 _ flagsUInt) -> dispatch_queue_t!

    OBJECTIVE-C

    dispatch_queue_t dispatch_get_global_queue ( long identifier, unsigned long flags );

    Parameters

    identifier

    The quality of service you want to give to tasks executed using this queue. Quality-of-service helps determine the priority given to tasks executed by the queue. You may specify the values QOS_CLASS_USER_INTERACTIVEQOS_CLASS_USER_INITIATEDQOS_CLASS_UTILITY, or QOS_CLASS_BACKGROUND. Queues that handle user-interactive or user-initiated tasks have a higher priority than tasks meant to run in the background.

    You may also specify one of the dispatch queue priority values, which are found in dispatch_queue_priority_t. These values map to an appropriate quality-of-service class. 

    flags

    Flags that are reserved for future use. Always specify 0 for this parameter. 

    Return Value

    The requested global concurrent queue.

    Discussion

    This function returns a queue suitable for executing tasks with the specified quality-of-service level. Calls to the dispatch_suspenddispatch_resume, and dispatch_set_contextfunctions have no effect on the returned queues.

    Tasks submitted to the returned queue are scheduled concurrently with respect to one another. 

    Import Statement

    import Dispatch

    Availability

    Available in iOS 4.0 and later.

  • dispatch_queue_create

    Creates a new dispatch queue to which blocks can be submitted.

    Declaration

    SWIFT

    func dispatch_queue_create(_ labelUnsafePointer<Int8>,
                             _ attrdispatch_queue_attr_t!) -> dispatch_queue_t!

    OBJECTIVE-C

    dispatch_queue_t dispatch_queue_create ( const char *label, dispatch_queue_attr_t attr );

    Parameters

    label

    A string label to attach to the queue to uniquely identify it in debugging tools such as Instruments, sample, stackshots, and crash reports. Because applications, libraries, and frameworks can all create their own dispatch queues, a reverse-DNS naming style (com.example.myqueue) is recommended. This parameter is optional and can be NULL.

    attr

    In OS X v10.7 and later or iOS 4.3 and later, specify DISPATCH_QUEUE_SERIAL (or NULL) to create a serial queue or specify DISPATCH_QUEUE_CONCURRENT to create a concurrent queue. In earlier versions, you must specify NULL for this parameter.

    Return Value

    The newly created dispatch queue. 

    Discussion

    Blocks submitted to a serial queue are executed one at a time in FIFO order. Note, however, that blocks submitted to independent queues may be executed concurrently with respect to each other. Blocks submitted to a concurrent queue are dequeued in FIFO order but may run concurrently if resources are available to do so.

    When your application no longer needs the dispatch queue, it should release it with the dispatch_release function. Any pending blocks submitted to a queue hold a reference to that queue, so the queue is not deallocated until all pending blocks have completed.

    Import Statement

    import Dispatch

    Availability

    Available in iOS 4.0 and later.

  • dispatch_get_current_queue (iOS 6.0)

    Returns the queue on which the currently executing block is running.

    Declaration

    OBJECTIVE-C

    dispatch_queue_t dispatch_get_current_queue ( void );

    Return Value

    Returns the current queue.

    Discussion

    This function is defined to never return NULL

    When called from outside of the context of a submitted block, this function returns the main queue if the call is executed from the main thread. If the call is made from any other thread, this function returns the default concurrent queue.

    Import Statement

    Availability

    Available in iOS 4.0 and later.

    Deprecated in iOS 6.0.


0 0
原创粉丝点击