队列queue.h方法总览
Queue相关其他方法
一、迭代执行任务
/**
dispatch_apply函数是dispatch_sync函数和Dispatch Group的关联API
该函数按指定的次数将指定的Block追加到指定的Dispatch Queue中,并等到全部的处理执行结束
@param iterations 指定重复次数
@param queue 追加对象的Dispatch Queue
@param index 带有参数的Block, index的作用是为了按执行的顺序区分各个Block
*/
void
dispatch_apply(size_t iterations, dispatch_queue_t queue,
DISPATCH_NOESCAPE void (^block)(size_t));
void
dispatch_apply_f(size_t iterations, dispatch_queue_t queue,
void *_Nullable context,
void (*work)(void *_Nullable, size_t));
举例说明:
//任务都是同步执行,会阻塞主线程(dispatch_apply是同步方法)
dispatch_queue_attr_t attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INITIATED, -1);
dispatch_queue_t queue = dispatch_queue_create("com.Maker", attr);
dispatch_apply(3, queue, ^(size_t index) {
for (int i = 0; i < 5; i++) {
if (index == 0) {
NSLog(@"任务内部顺序----%d 任务顺序====%zu", i,index);
}
else if (index == 1) {
NSLog(@"任务内部顺序----%d 任务顺序~~~~%zu", i,index);
}
else if (index == 2) {
NSLog(@"任务内部顺序----%d 任务顺序____%zu", i,index);
}
else {
NSLog(@"任务内部顺序----%d 任务顺序++++%zu", i,index);
}
}
});
二、延迟执行
/**
延迟执行任务
@param when 延迟执行的时间
@param queue 指定在某一队列中延迟执行
@param block 执行的具体任务
*/
void
dispatch_after(dispatch_time_t when,
dispatch_queue_t queue,
dispatch_block_t block);
/**
延迟执行任务
@param when 延迟执行的时间
@param queue 指定在某一队列中延迟执行
@param context 传递给work的任意类型(void *)参数
@param work 执行的C语言函数名(函数指针)
*/
void
dispatch_after_f(dispatch_time_t when,
dispatch_queue_t queue,
void *_Nullable context,
dispatch_function_t work);
举例说明:
//这个任务将会在10秒后执行
/*----dispatch_after----*/
int delayInSeconds = 10;
dispatch_time_t delaytime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(delaytime, dispatch_get_main_queue(), ^{
//任务
});
/*----dispatch_after_f----*/
int context = 10;
int delayInSeconds = 10;
dispatch_time_t delaytime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after_f(delaytime, dispatch_get_main_queue(), &context, logCount);
void logCount(void* context) {
int *c = context;
NSLog(@"内存地址 === %p 值 === %d", &c,*c);
}
三、设置队列属性
/**
队列属性(串行/并发、优先级等)
@param attr 队列属性(DISPATCH_QUEUE_SERIAL / DISPATCH_QUEUE_CONCURRENT)
*/
dispatch_queue_attr_t
dispatch_queue_attr_make_initially_inactive(
dispatch_queue_attr_t _Nullable attr);
/**
队列属性(串行/并发、优先级等)
@param attr 队列属性(DISPATCH_QUEUE_SERIAL / DISPATCH_QUEUE_CONCURRENT)
@param frequency
*/
dispatch_queue_attr_t
dispatch_queue_attr_make_with_autorelease_frequency(
dispatch_queue_attr_t _Nullable attr,
dispatch_autorelease_frequency_t frequency);
/**
队列属性(串行/并发、优先级等)
@param attr 队列属性(DISPATCH_QUEUE_SERIAL / DISPATCH_QUEUE_CONCURRENT)
@param qos_class
@param relative_priority
*/
dispatch_queue_attr_t
dispatch_queue_attr_make_with_qos_class(dispatch_queue_attr_t _Nullable attr,
dispatch_qos_class_t qos_class, int relative_priority);
三、其他相关方法(不常用)
const char *
dispatch_queue_get_label(dispatch_queue_t _Nullable queue);
dispatch_qos_class_t
dispatch_queue_get_qos_class(dispatch_queue_t queue,
int *_Nullable relative_priority_ptr);
void
dispatch_set_target_queue(dispatch_object_t object,
dispatch_queue_t _Nullable queue);
void
dispatch_main(void);
void
dispatch_queue_set_specific(dispatch_queue_t queue, const void *key,
void *_Nullable context, dispatch_function_t _Nullable destructor);
void *_Nullable
dispatch_queue_get_specific(dispatch_queue_t queue, const void *key);
void *_Nullable
dispatch_get_specific(const void *key);
void
dispatch_assert_queue_barrier(dispatch_queue_t queue);