一:base.h
二:block.h
1.
dispatch_block_flags:DISPATCH_BLOCK_BARRIER暑竟,DISPATCH_BLOCK_DETACHED垃帅,DISPATCH_BLOCK_ASSIGN_CURRENT田盈,DISPATCH_BLOCK_NO_QOS_CLASS,DISPATCH_BLOCK_INHERIT_QOS_CLASS樟结,DISPATCH_BLOCK_ENFORCE_QOS_CLASS
2.
dispatch_block_t dispatch_block_create(dispatch_block_flags_t flags, dispatch_block_t block);
說明:
創(chuàng)建block的一種方式瘤缩,flags?參數(shù)用來設(shè)置?block?的標(biāo)記,block?參數(shù)用來設(shè)置具體的任務(wù)檀轨。
實(shí)例:
dispatch_block_t block = dispatch_block_create(0, ^{
? ? ? ? NSLog(@"hello");
? ? });
block();
3.
dispatch_block_t dispatch_block_create_with_qos_class(dispatch_block_flags_t flags,dispatch_qos_class_t qos_class,int relative_priority,dispatch_block_t block);
說明:
創(chuàng)建block的另一種方式,這種方式在創(chuàng)建?block?的同時(shí)可以指定了相應(yīng)的優(yōu)先級欺嗤。dispatch_qos_class_t?是?qos_class_t?的別名参萄。qos_class_t是一種枚舉,有以下類型:
QOS_CLASS_USER_INTERACTIVE:user interactive等級表示任務(wù)需要被立即執(zhí)行煎饼,用來在響應(yīng)事件之后更新 UI讹挎,來提供好的用戶體驗(yàn)。這個(gè)等級最好保持小規(guī)模腺占。
QOS_CLASS_USER_INITIATED:user initiated等級表示任務(wù)由 UI 發(fā)起異步執(zhí)行淤袜。適用場景是需要及時(shí)結(jié)果同時(shí)又可以繼續(xù)交互的時(shí)候。
QOS_CLASS_DEFAULT:default默認(rèn)優(yōu)先級
QOS_CLASS_UTILITY:utility等級表示需要長時(shí)間運(yùn)行的任務(wù)衰伯,伴有用戶可見進(jìn)度指示器铡羡。經(jīng)常會(huì)用來做計(jì)算,I/O意鲸,網(wǎng)絡(luò)烦周,持續(xù)的數(shù)據(jù)填充等任務(wù)。這個(gè)任務(wù)節(jié)能怎顾。
QOS_CLASS_BACKGROUND:background等級表示用戶不會(huì)察覺的任務(wù)读慎,使用它來處理預(yù)加載,或者不需要用戶交互和對時(shí)間不敏感的任務(wù)槐雾。
QOS_CLASS_UNSPECIFIED:unspecified未指明
實(shí)例:
dispatch_block_t qosBlock = dispatch_block_create_with_qos_class(0, QOS_CLASS_DEFAULT,0, ^{NSLog(@"qos do some thing...");});
4.
void dispatch_block_perform(dispatch_block_flags_t flags,DISPATCH_NOESCAPE dispatch_block_t block);
說明:
可能是 創(chuàng)建夭委,執(zhí)行一個(gè)block,并自行銷毀。等同以下代碼募强,dispatch_block_t b = dispatch_block_create(flags, block);b();Block_release(b);
實(shí)例:
dispatch_block_perform(0, ^{
? ? ? ? UIImageView* imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,100,300,300)];
? ? ? ? imageView.backgroundColor= [UIColorgrayColor];
? ? ? ? NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://lvpfileserver.iuoooo.com/Jinher.JAP.BaseApp.FileServer.UI/FileManage/GetFile?fileURL=49e54e46-3e17-4ca4-8f03-db71fb8f9655/2017081910/09b30913-e4bc-4218-9d0f-8afc05e49cc6_86b45da2-22ac-4194-945a-80a1f7586fb8.jpg"]];
? ? ? ? UIImage* image = [UIImageimageWithData:data];
? ? ? ? imageView.image= image;
? ? ? ? [self.viewaddSubview:imageView];
? ? });
5.
long dispatch_block_wait(dispatch_block_tblock,dispatch_time_ttimeout);
說明:
同步等待株灸,直到代碼塊執(zhí)行完成或者超時(shí)。如果代碼塊在指定的時(shí)間內(nèi)完成擎值,則返回零;如果塊超時(shí)慌烧,則返回非零值。該函數(shù)會(huì)阻塞當(dāng)前線程進(jìn)行等待鸠儿。傳入需要設(shè)置的block和等待時(shí)間timeout屹蚊。timeout參數(shù)表示函數(shù)在等待block執(zhí)行完畢時(shí)厕氨,應(yīng)該等待多久。如果執(zhí)行block所需的時(shí)間小于timeout汹粤,則返回 0命斧,否則返回非 0 值。此參數(shù)也可以取常量DISPATCH_TIME_FOREVER玄括,這表示函數(shù)會(huì)一直等待block執(zhí)行完冯丙,而不會(huì)超時(shí)≡饩可以使用dispatch_time函數(shù)和DISPATCH_TIME_NOW常量來方便的設(shè)置具體的超時(shí)時(shí)間。
如果block執(zhí)行完成泞莉,dispatch_block_wait就會(huì)立即返回哪雕。不能使用dispatch_block_wait來等待同一個(gè)block的多次執(zhí)行全部結(jié)束;這種情況可以考慮使用dispatch_group_wait來解決鲫趁。也不能在多個(gè)線程中斯嚎,同時(shí)等待同一個(gè)block的結(jié)束。同一個(gè)block只能執(zhí)行一次挨厚,被等待一次堡僻。
注意:因?yàn)閐ispatch_block_wait會(huì)阻塞當(dāng)前線程,所以不應(yīng)該放在主線程中調(diào)用疫剃。
有時(shí)我們需要等待特定的?block?執(zhí)行完成之后钉疫,再去執(zhí)行其他任務(wù)。有兩種方法可以獲取到指定?block?執(zhí)行結(jié)束的時(shí)機(jī),dispatch_block_wait是其中一種巢价,另一種是方法是dispatch_block_notify牲阁。
實(shí)例:
dispatch_queue_t concurrentQuene = dispatch_queue_create("concurrentQuene", DISPATCH_QUEUE_CONCURRENT);
? ? dispatch_async(concurrentQuene, ^{
? ? ? ? dispatch_queue_tallTasksQueue =dispatch_queue_create("allTasksQueue",DISPATCH_QUEUE_CONCURRENT);
? ? ? ? dispatch_block_tblock =dispatch_block_create(0, ^{
? ? ? ? ? ? NSLog(@"開始執(zhí)行");
? ? ? ? ? ? [NSThread sleepForTimeInterval:3];
? ? ? ? ? ? NSLog(@"結(jié)束執(zhí)行");
? ? ? ? });
? ? ? ? dispatch_async(allTasksQueue, block);
? ? ? ? // 等待時(shí)長,10s 之后超時(shí)
? ? ? ? dispatch_time_ttimeout =dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10*NSEC_PER_SEC));
? ? ? ? longresutl =dispatch_block_wait(block, timeout);
? ? ? ? if(resutl ==0) {
? ? ? ? ? ? NSLog(@"執(zhí)行成功");
? ? ? ? }else{
? ? ? ? ? ? NSLog(@"執(zhí)行超時(shí)");
? ? ? ? }
? ? });
6.
void dispatch_block_notify(dispatch_block_t block,dispatch_queue_t queue,dispatch_block_t notification_block);
說明:
該函數(shù)接收三個(gè)參數(shù)壤躲,第一個(gè)參數(shù)是需要監(jiān)視的block城菊,第二個(gè)參數(shù)是監(jiān)聽的block執(zhí)行結(jié)束之后要提交執(zhí)行的隊(duì)列queue,第三個(gè)參數(shù)是待加入到隊(duì)列中的block碉克。 和dispatch_block_wait的不同之處在于:dispatch_block_notify函數(shù)不會(huì)阻塞當(dāng)前線程凌唬。
實(shí)例:
NSLog(@"---- 開始設(shè)置任務(wù) ----");
dispatch_queue_tserialQueue =? dispatch_queue_create("com.fyf.serialqueue",? DISPATCH_QUEUE_SERIAL);
? ? // 耗時(shí)任務(wù)
? ? dispatch_block_ttaskBlock =dispatch_block_create(0, ^{
? ? ? ? NSLog(@"開始耗時(shí)任務(wù)");
? ? ? ? [NSThread sleepForTimeInterval:2.f];
? ? ? ? NSLog(@"完成耗時(shí)任務(wù)");
? ? });
dispatch_async(serialQueue, taskBlock);
? ? // 更新 UI
? ? dispatch_block_trefreshUI =dispatch_block_create(0, ^{
? ? ? ? NSLog(@"更新 UI");
? ? });
// 設(shè)置監(jiān)聽
? ? dispatch_block_notify(taskBlock, dispatch_get_main_queue(), refreshUI);
? ? NSLog(@"---- 完成設(shè)置任務(wù) ----");
7.
void dispatch_block_cancel(dispatch_block_tblock);
說明:
這個(gè)函數(shù)用異步的方式取消指定的block。取消操作使將來執(zhí)行dispatch block會(huì)立即返回漏麦,但是對已經(jīng)在執(zhí)行的dispatch block沒有任何影響客税。當(dāng)一個(gè)block被取消時(shí),它會(huì)立即釋放捕獲的資源唁奢。如果要在一個(gè)block中對某些對象進(jìn)行釋放操作霎挟,在取消這個(gè)block的時(shí)候,需要確保內(nèi)存不會(huì)泄漏麻掸。
實(shí)例:
dispatch_queue_tserialQueue = dispatch_queue_create("com.fyf.serialqueue", DISPATCH_QUEUE_SERIAL);// 耗時(shí)任務(wù)dispatch_block_t firstTaskBlock = dispatch_block_create(0, ^{NSLog(@"開始第一個(gè)任務(wù)"); [NSThreadsleepForTimeInterval:1.5f];NSLog(@"結(jié)束第一個(gè)任務(wù)");});// 耗時(shí)任務(wù)dispatch_block_t secTaskBlock = dispatch_block_create(0, ^{NSLog(@"開始第二個(gè)任務(wù)"); [NSThreadsleepForTimeInterval:2.f];NSLog(@"結(jié)束第二個(gè)任務(wù)");});dispatch_async(serialQueue, firstTaskBlock);dispatch_async(serialQueue, secTaskBlock);// 等待 1s酥夭,讓第一個(gè)任務(wù)開始運(yùn)行[NSThreadsleepForTimeInterval:1];dispatch_block_cancel(firstTaskBlock);NSLog(@"嘗試過取消第一個(gè)任務(wù)");dispatch_block_cancel(secTaskBlock);NSLog(@"嘗試過取消第二個(gè)任務(wù)");
8.
long dispatch_block_testcancel(dispatch_block_t block);
說明:
測試是否已取消給定的調(diào)度塊。如果成功取消代碼塊,則返回非零值熬北,否則返回零疙描。該函數(shù)的作用是讓我們能夠知道當(dāng)前任務(wù)塊是否已經(jīng)被取消。
實(shí)例:
返回值經(jīng)常錯(cuò)誤
三:data.h
1.
dispatch_data_t dispatch_data_create(constvoid*buffer,size_tsize,dispatch_queue_t _Nullable queue,dispatch_block_t _Nullable destructor);
說明:
dispatch_data_t讶隐,表示連續(xù)或稀疏內(nèi)存區(qū)域的不可變對象起胰。
dispatch_data_create,使用指定的內(nèi)存緩沖區(qū)創(chuàng)建新的代碼塊數(shù)據(jù)對象。
實(shí)例:
2.
size_t dispatch_data_get_size(dispatch_data_t data);
說明:
返回代碼塊數(shù)據(jù)對象的內(nèi)存大小巫延。
實(shí)例:
3.
dispatch_data_t dispatch_data_create_map(dispatch_data_t data,const void *_Nullable *_Nullable buffer_ptr,size_t*_Nullablesize_ptr);
說明:
返回一個(gè)新的調(diào)度數(shù)據(jù)對象效五,其中包含指定對象內(nèi)存的連續(xù)表示。
實(shí)例:
4.
dispatch_data_t dispatch_data_create_concat(dispatch_data_t data1,dispatch_data_t data2);
說明:
返回一個(gè)新的調(diào)度數(shù)據(jù)對象炉峰,該對象由來自其他兩個(gè)數(shù)據(jù)對象的連接數(shù)據(jù)組成畏妖。
實(shí)例:
5.
dispatch_data_t dispatch_data_create_subrange(dispatch_data_t data,size_toffset,size_tlength);
說明:
返回一個(gè)新的調(diào)度數(shù)據(jù)對象,其內(nèi)容由另一個(gè)對象的內(nèi)存區(qū)域的一部分組成疼阔。
實(shí)例:
6.
typedef bool(^dispatch_data_applier_t)(dispatch_data_tregion,size_toffset,constvoid*buffer,size_tsize);
說明:
遍歷戒劫。
實(shí)例:
7.
bool dispatch_data_apply(dispatch_data_tdata,DISPATCH_NOESCAPE dispatch_data_applier_t applier);
說明:
遍歷調(diào)度數(shù)據(jù)對象的內(nèi)存并在每個(gè)區(qū)域上執(zhí)行自定義代碼。?
實(shí)例:
8.
dispatch_data_t dispatch_data_copy_region(dispatch_data_t data,size_tlocation,size_t*offset_ptr);
說明:
返回包含另一個(gè)數(shù)據(jù)對象中的一部分?jǐn)?shù)據(jù)的數(shù)據(jù)對象婆廊。
實(shí)例:
四:dispatch.h
五:group.h
1.
dispatch_group_t dispatch_group_create(void);
說明:
dispatch_group_t迅细,提交到隊(duì)列以進(jìn)行異步調(diào)用的一組塊對象。
dispatch_group_create淘邻,創(chuàng)建一個(gè)可以與塊對象關(guān)聯(lián)的調(diào)度組茵典。
實(shí)例:
2.
void dispatch_group_async(dispatch_group_t group,dispatch_queue_t queue,dispatch_block_t block);
說明:
將塊提交到調(diào)度隊(duì)列,并將塊與指定的調(diào)度組關(guān)聯(lián)列荔。
實(shí)例:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
? ? dispatch_group_t?group?=?dispatch_group_create();
? ? dispatch_group_async(group,?queue,?^{
? ? ? ? [NSThread?sleepForTimeInterval:1];
? ? ? ? NSLog(@"group1");
? ? });
? ? dispatch_group_async(group,?queue,?^{
? ? ? ? [NSThread?sleepForTimeInterval:2];
? ? ? ? NSLog(@"group2");
? ? });
? ? dispatch_group_async(group,?queue,?^{
? ? ? ? [NSThread?sleepForTimeInterval:3];
? ? ? ? NSLog(@"group3");
? ? });
? ? dispatch_group_notify(group,?dispatch_get_main_queue(),?^{
? ? ? ? NSLog(@"updateUi");
? ? });
3.
void dispatch_group_async_f(dispatch_group_tgroup,dispatch_queue_t queue,void *_Nullable context,dispatch_function_t work);
說明:
將應(yīng)用程序定義的函數(shù)提交給調(diào)度隊(duì)列敬尺,并將其與指定的調(diào)度組關(guān)聯(lián)。
實(shí)例:
4.
long dispatch_group_wait(dispatch_group_t group,dispatch_time_t timeout);
說明:
同步等待先前提交的塊對象完成;?如果塊在指定的超時(shí)時(shí)間段之前未完成贴浙,則返回砂吞。成功時(shí)返回零(與指定超時(shí)之前完成的組關(guān)聯(lián)的所有塊)或錯(cuò)誤時(shí)返回非零(發(fā)生超時(shí))。
實(shí)例:
5.
void dispatch_group_notify(dispatch_group_t group,dispatch_queue_t queue,dispatch_block_t block);
說明:
在一組先前提交的塊對象完成時(shí)崎溃,計(jì)劃要將塊對象??提交到隊(duì)列蜻直。
實(shí)例:
6.
void dispatch_group_notify_f(dispatch_group_t group,dispatch_queue_t queue,void *_Nullable context,dispatch_function_t work);
說明:
計(jì)劃在一組先前提交的塊對象完成時(shí)將應(yīng)用程序定義的函數(shù)提交到隊(duì)列。
實(shí)例:
7.
void dispatch_group_enter(dispatch_group_tgroup);
說明:
顯式指示塊已進(jìn)入組袁串。
實(shí)例:
8.
void dispatch_group_leave(dispatch_group_tgroup);
說明:
明確指示組中的塊已完成概而。調(diào)用此函數(shù)會(huì)減少組中未完成任務(wù)的當(dāng)前計(jì)數(shù)。使用此函數(shù)(with?)允許應(yīng)用程序正確管理任務(wù)引用計(jì)數(shù)囱修,如果它通過除使用函數(shù)之外的方式顯式添加和刪除組中的任務(wù)赎瑰。
六:introspection.h
1.
void
dispatch_introspection_hook_queue_create(dispatch_queue_t queue);
2.
void
dispatch_introspection_hook_queue_destroy(dispatch_queue_t queue);
3.
void
dispatch_introspection_hook_queue_item_enqueue(dispatch_queue_t queue,
dispatch_object_t item);
4.
void
dispatch_introspection_hook_queue_item_dequeue(dispatch_queue_t queue,
dispatch_object_t item);
5.
void
dispatch_introspection_hook_queue_item_complete(dispatch_object_t item);
6.
void
dispatch_introspection_hook_queue_callout_begin(dispatch_queue_t queue,
void*_Nullablecontext, dispatch_function_t function);
7.
void
dispatch_introspection_hook_queue_callout_end(dispatch_queue_t queue,
void*_Nullablecontext, dispatch_function_t function);
七:io.h
1.
typedef int dispatch_fd_t;
說明;
用于I / O操作的文件描述符。
2.
void dispatch_read(dispatch_fd_t fd,size_t length,dispatch_queue_t queue,void(^handler)(dispatch_data_t data,int error));
說明:
使用指定的文件描述符計(jì)劃異步讀取操作破镰。
3.
void dispatch_write(dispatch_fd_t fd,dispatch_data_t data,dispatch_queue_t queue,void(^handler)(dispatch_data_t_Nullabledata,interror));
說明:
使用指定的文件描述符計(jì)劃異步寫入操作餐曼。
4.
typedef unsigned long dispatch_io_type_t;
說明:
調(diào)度I / O通道的類型
5.
dispatch_io_t dispatch_io_create(dispatch_io_type_t type,dispatch_fd_t fd,dispatch_queue_t queue,void(^cleanup_handler)(interror));
說明:
創(chuàng)建調(diào)度I / O通道并將其與指定的文件描述符關(guān)聯(lián)压储。?
6.
dispatch_io_t dispatch_io_create_with_path(dispatch_io_type_t type,const char *path,int oflag,mode_t mode,dispatch_queue_t queue,void(^cleanup_handler)(interror));
說明:
使用關(guān)聯(lián)的路徑名創(chuàng)建調(diào)度I / O通道。
7.
dispatch_io_t dispatch_io_create_with_io(dispatch_io_type_t type,dispatch_io_t io,dispatch_queue_t queue,void(^cleanup_handler)(interror));
說明:
從現(xiàn)有通道創(chuàng)建新的調(diào)度I / O通道源譬。
8.
typedef void(^dispatch_io_handler_t)(bool done,dispatch_data_t_Nullabledata,
int error);
說明:
用于處理調(diào)度I / O通道上的操作的處理程序塊集惋。調(diào)度I / O處理程序的參數(shù)如下:
done?- 表示操作是否完成的標(biāo)志。
data?- 要處理的數(shù)據(jù)對象踩娘。系統(tǒng)會(huì)在處理程序執(zhí)行期間保留此對象刮刑,并在處理程序塊返回時(shí)釋放該對象。
error?- 報(bào)告操作的錯(cuò)誤編號(如果有)养渴。錯(cuò)誤號0通常表示操作成功雷绢。
9.
void dispatch_io_read(dispatch_io_t channel,off_toffset,size_tlength,dispatch_queue_t queue,dispatch_io_handler_t io_handler);
說明:
在指定通道上計(jì)劃異步讀取操作。
10.
void dispatch_io_write(dispatch_io_t channel,off_t offset,dispatch_data_t data,dispatch_queue_t queue,dispatch_io_handler_t io_handler);
說明:
為指定的通道安排異步寫入操作厚脉。
11.
typedef unsigned long dispatch_io_close_flags_t;
說明:
用于指定通道關(guān)閉選項(xiàng)的標(biāo)志類型习寸。
12.
void dispatch_io_close(dispatch_io_t channel, dispatch_io_close_flags_t flags);
說明:
將指定通道關(guān)閉到新的讀寫操作。
13.
void dispatch_io_barrier(dispatch_io_t channel,dispatch_block_t barrier);
說明:
在指定的通道上安排屏障操作傻工。
14.
dispatch_fd_t dispatch_io_get_descriptor(dispatch_io_t channel);
說明:
返回與指定通道關(guān)聯(lián)的文件描述符。
15.
void dispatch_io_set_high_water(dispatch_io_t channel,size_t high_water);
說明:
設(shè)置在排隊(duì)處理程序塊之前要處理的最大字節(jié)數(shù)孵滞。
16.
void dispatch_io_set_low_water(dispatch_io_t channel,size_t low_water);
說明:
設(shè)置在排隊(duì)處理程序塊之前要處理的最小字節(jié)數(shù)中捆。
17.
typedef unsigned long dispatch_io_interval_flags_t;
說明:
用于指定通道的調(diào)度間隔的標(biāo)志的類型。
18坊饶。
void dispatch_io_set_interval(dispatch_io_t channel,uint64_t interval,dispatch_io_interval_flags_t flags);
說明:
設(shè)置調(diào)用通道的I / O處理程序的間隔(以納秒為單位)泄伪。
八:object.h
1.
void _dispatch_object_validate(dispatch_object_tobject) {
void*isa = *(void*volatile*)(OS_OBJECT_BRIDGEvoid*)object;
(void)isa;
}
2.
typedef void(^dispatch_block_t)(void);
3.
void dispatch_retain(dispatch_object_t object);
說明:
增加調(diào)度對象的引用(保留)計(jì)數(shù)。
4.
void dispatch_release(dispatch_object_t object);
說明:
減少調(diào)度對象的引用(保留)計(jì)數(shù)匿级。
5.
void *_Nullable dispatch_get_context(dispatch_object_t object);
說明:
返回對象的應(yīng)用程序定義的上下文蟋滴。
6.
void dispatch_set_context(dispatch_object_t object,void *_Nullable context);
說明:
將應(yīng)用程序定義的上下文與對象相關(guān)聯(lián)。
7.
void dispatch_set_finalizer_f(dispatch_object_t object,dispatch_function_t _Nullable finalizer);
說明:
設(shè)置調(diào)度對象的終結(jié)器函數(shù)痘绎。
8.
void dispatch_activate(dispatch_object_t object);
說明:
激活調(diào)度對象津函。
9.
void dispatch_suspend(dispatch_object_t object);
說明:
暫停調(diào)度對象上塊對象的調(diào)用。
10.
void dispatch_resume(dispatch_object_t object);
說明:
恢復(fù)調(diào)度對象上塊對象的調(diào)用孤页。
11.
long dispatch_wait(void*object,dispatch_time_t timeout);
說明:
同步等待先前提交的塊對象完成;?如果塊在指定的超時(shí)時(shí)間段之前未完成尔苦,則返回。成功時(shí)返回零(與指定超時(shí)之前完成的組關(guān)聯(lián)的所有塊)或錯(cuò)誤時(shí)返回非零(發(fā)生超時(shí))行施。
12.
void dispatch_notify(void*object,dispatch_object_t queue,dispatch_block_t notification_block);
說明:
在一組先前提交的塊對象完成時(shí)允坚,計(jì)劃要將塊對象??提交到隊(duì)列。
13.
void dispatch_cancel(void*object);
說明:
異步取消指定的調(diào)度塊蛾号。
14.
long dispatch_testcancel(void*object);
說明:
測試是否已取消給定的調(diào)度塊稠项。
15.
void dispatch_debug(dispatch_object_t object,const char * message, ...);
說明:
以編程方式記錄有關(guān)調(diào)度對象的調(diào)試信息。已棄用鲜结。
16.
void dispatch_debugv(dispatch_object_t object,const char * message,va_list ap);
說明:
已棄用展运。
九:once.h
1.
typedef long dispatch_once_t;
說明:
與函數(shù)一起使用的謂詞活逆。此類型的變量必須具有全局或靜態(tài)范圍。使用此類型進(jìn)行自動(dòng)或動(dòng)態(tài)分配的結(jié)果是未定義的乐疆。
2.
void dispatch_once(dispatch_once_t*predicate,DISPATCH_NOESCAPE dispatch_block_t block);
說明:
在應(yīng)用程序的生命周期內(nèi)執(zhí)行一次且僅執(zhí)行一次塊對象划乖。
3.
void _dispatch_once(dispatch_once_t*predicate,DISPATCH_NOESCAPE dispatch_block_t block){
if(DISPATCH_EXPECT(*predicate, ~0l) != ~0l) {
dispatch_once(predicate, block);
}else{
dispatch_compiler_barrier();
}
DISPATCH_COMPILER_CAN_ASSUME(*predicate == ~0l);
}
4.
void dispatch_once_f(dispatch_once_t*predicate,void*_Nullablecontext,
dispatch_function_t function);
說明:
在應(yīng)用程序的生命周期內(nèi)只執(zhí)行一次應(yīng)用程序定義的函數(shù)。
5.
void _dispatch_once_f(dispatch_once_t*predicate,void*_Nullablecontext,
dispatch_function_t function)
{
if(DISPATCH_EXPECT(*predicate, ~0l) != ~0l) {
dispatch_once_f(predicate, context, function);
}else{
dispatch_compiler_barrier();
}
DISPATCH_COMPILER_CAN_ASSUME(*predicate == ~0l);
}
十:queue.h
1.
void dispatch_async(dispatch_queue_t queue, dispatch_block_t block);
說明:
在調(diào)度隊(duì)列上提交異步執(zhí)行塊并立即返回挤土。queue琴庵,提交塊的隊(duì)列仰美。系統(tǒng)保留隊(duì)列迷殿,直到塊運(yùn)行完成。這個(gè)參數(shù)不能NULL咖杂。block庆寺,要提交到目標(biāo)調(diào)度隊(duì)列的塊。 此函數(shù)代表調(diào)用者執(zhí)行Block_copy和Block_release诉字。 此參數(shù)不能為NULL懦尝。調(diào)用此函數(shù)后,始終在提交塊后立即返回壤圃,并且永遠(yuǎn)不會(huì)等待調(diào)用該塊陵霉。目標(biāo)隊(duì)列確定是否相對于提交到同一隊(duì)列的其他塊串行或同時(shí)調(diào)用該塊。獨(dú)立的串行隊(duì)列相互同時(shí)處理伍绳。
實(shí)例:
NSLog(@"123");
? ? dispatch_async(dispatch_get_global_queue(0, 0), ^{
? ? ? ? NSLog(@"456");
? ? });
? ? NSLog(@"789");
打佑荒印:123,789冲杀,456
2.
void dispatch_async_f(dispatch_queue_t queue,void *_Nullable context,dispatch_function_t work);
說明:
提交應(yīng)用程序定義的函數(shù)以在調(diào)度隊(duì)列上進(jìn)行異步執(zhí)行并立即返回效床。queue,要提交功能的隊(duì)列权谁。系統(tǒng)保留隊(duì)列剩檀,直到該功能運(yùn)行完成。這個(gè)參數(shù)不能NULL闯传。context谨朝,應(yīng)用程序定義的上下文參數(shù)傳遞給函數(shù)。work甥绿,應(yīng)用程序定義的函數(shù)字币,用于在目標(biāo)隊(duì)列上調(diào)用。傳遞給此函數(shù)的第一個(gè)參數(shù)是context參數(shù)的值共缕。這個(gè)參數(shù)不能NULL洗出。
3.
void dispatch_sync(dispatch_queue_t queue, DISPATCH_NOESCAPE dispatch_block_t block);
說明:
提交塊對象以在調(diào)度隊(duì)列上執(zhí)行,并等待該塊完成图谷。queue翩活,提交塊的隊(duì)列阱洪。這個(gè)參數(shù)不能NULL。block菠镇,要在目標(biāo)調(diào)度隊(duì)列上調(diào)用的塊冗荸。這個(gè)參數(shù)不能NULL。
實(shí)例:
NSLog(@"123");
? ? dispatch_sync(dispatch_get_global_queue(0, 0), ^{
? ? ? ? NSLog(@"456");
? ? });
? ? NSLog(@"789");
打永!:123蚌本,456,789
4.
void dispatch_sync_f(dispatch_queue_t queue,void *_Nullable context,dispatch_function_t work);
說明:
提交應(yīng)用程序定義的函數(shù)以在調(diào)度隊(duì)列上執(zhí)行同步隘梨。
5.
void dispatch_apply(size_t iterations,dispatch_queue_t queue,DISPATCH_NOESCAPE void (^block)(size_t));
說明:
將塊提交給調(diào)度隊(duì)列以進(jìn)行多次調(diào)用程癌。
size_t iterations: 執(zhí)行的次數(shù)
dispatch_queue_t queue: 提交的隊(duì)列
block 執(zhí)行的任務(wù)
size_t block中每次任務(wù)執(zhí)行的索引
實(shí)例:
dispatch_apply(5, dispatch_get_global_queue(0, 0), ^(size_t index) {
? ? ? ? NSLog(@"66666,%ld",index);
? ? });
打印:66666,0轴猎;66666,1嵌莉;66666,2;66666,3捻脖;66666,4
6.
void dispatch_apply_f(size_t iterations,dispatch_queue_t queue,void *_Nullable context,void (*work)(void *_Nullable,size_t));
說明:
將應(yīng)用程序定義的函數(shù)提交給多個(gè)調(diào)用的調(diào)度隊(duì)列锐峭。
7.
dispatch_queue_t dispatch_get_current_queue(void);
說明:
返回正在運(yùn)行當(dāng)前正在執(zhí)行的塊的隊(duì)列。已棄用可婶。
8.
dispatch_queue_t dispatch_get_main_queue(void)
{
return DISPATCH_GLOBAL_OBJECT(dispatch_queue_t, _dispatch_main_q);
}
說明:
返回與應(yīng)用程序主線程關(guān)聯(lián)的串行調(diào)度隊(duì)列只祠。
9.
typedef long dispatch_queue_priority_t;
10.
typedef qos_class_t dispatch_qos_class_t;
11.
typedef unsigned int dispatch_qos_class_t;
12.
dispatch_queue_t dispatch_get_global_queue(long identifier,unsigned long flags);
說明:
返回具有指定服務(wù)質(zhì)量類的系統(tǒng)定義的全局并發(fā)隊(duì)列。
13.
struct dispatch_queue_attr_s _dispatch_queue_attr_concurrent;
14.
dispatch_queue_attr_t dispatch_queue_attr_make_initially_inactive(dispatch_queue_attr_t _Nullable attr);
15.
dispatch_queue_attr_t dispatch_queue_attr_make_with_autorelease_frequency(dispatch_queue_attr_t _Nullable attr,dispatch_autorelease_frequency_t frequency);
16.
dispatch_queue_attr_t dispatch_queue_attr_make_with_qos_class(dispatch_queue_attr_t_Nullableattr,dispatch_qos_class_tqos_class,intrelative_priority);
說明:
創(chuàng)建帶有優(yōu)先級的dispatch_queue_attr_t對象扰肌。通過這個(gè)對象可以自定義queue的優(yōu)先級。
17.
dispatch_queue_t dispatch_queue_create_with_target(constchar*_Nullablelabel,dispatch_queue_attr_t _Nullable attr,dispatch_queue_t _Nullable target)DISPATCH_ALIAS_V2(dispatch_queue_create_with_target);
18.
dispatch_queue_t dispatch_queue_create(constchar*_Nullablelabel,dispatch_queue_attr_t _Nullable attr);
說明:
創(chuàng)建可以提交塊的新調(diào)度隊(duì)列熊杨。
19.
const char * dispatch_queue_get_label(dispatch_queue_t_Nullablequeue);
說明:
返回創(chuàng)建隊(duì)列時(shí)為隊(duì)列指定的標(biāo)簽曙旭。
20.
dispatch_qos_class_t dispatch_queue_get_qos_class(dispatch_queue_t queue,int*_Nullablerelative_priority_ptr);
21.
void dispatch_set_target_queue(dispatch_object_t object,dispatch_queue_t _Nullable queue);
說明:
設(shè)置給定對象的目標(biāo)隊(duì)列。
用法一:改變dispatch_queue_create函數(shù)生成的Dispatch Queue的優(yōu)先級晶府。用法二:作成Dispatch Queue的執(zhí)行階層
如果在多個(gè)Serial Dispatch Queue中用dispatch_set_target_queue函數(shù)指定目標(biāo)位某一個(gè)Serial Dispatch Queue,那么原先本應(yīng)并行執(zhí)行的多個(gè)Serial Dispatch Queue桂躏,在目標(biāo)Serial Dispatch Queue上只能同時(shí)執(zhí)行一個(gè)處理。即先執(zhí)行完一個(gè)隊(duì)列里的任務(wù)川陆,再執(zhí)行另一個(gè)隊(duì)列的任務(wù)剂习。
實(shí)例:
1.
dispatch_queue_tserialQueue = dispatch_queue_create("com.ios.oc",NULL);
dispatch_queue_tglobalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0); dispatch_set_target_queue(serialQueue, globalQueue);
2.
dispatch_queue_tserialQueue1 = dispatch_queue_create("com.ios.chen",NULL);
dispatch_queue_tserialQueue2 = dispatch_queue_create("com.ios.zhen",NULL);
dispatch_queue_ttargetQueue = dispatch_queue_create("com.ios.target",NULL); dispatch_set_target_queue(serialQueue1, targetQueue);?
?dispatch_set_target_queue(serialQueue2, targetQueue);
dispatch_async(serialQueue1, ^{NSLog(@"s1 in");
?[NSThreadsleepForTimeInterval:1.f];NSLog(@"s1 out"); });
dispatch_async(serialQueue2, ^{NSLog(@"s2 in");?
?[NSThreadsleepForTimeInterval:1.f];NSLog(@"s2 out"); });
打印:
s1 in较沪;s1 out鳞绕;s2 in;s2 out尸曼;
如果使用dispatch_set_target_queue,打印s1 in们何;s2 in;s2 out控轿;s1 out冤竹;
22.
void dispatch_main(void);
說明:
執(zhí)行提交到主隊(duì)列的塊拂封。
23.
void dispatch_after(dispatch_time_t when,dispatch_queue_t queue,dispatch_block_t block);
說明:
延遲一段時(shí)間把一項(xiàng)任務(wù)提交到隊(duì)列中執(zhí)行,返回之后就不能取消鹦蠕,常用來在在主隊(duì)列上延遲執(zhí)行一項(xiàng)任務(wù)冒签。
實(shí)例:
NSLog(@"開始");
? ? dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
? ? ? ? NSLog(@"12345");
? ? });
24.
void dispatch_after_f(dispatch_time_t when,dispatch_queue_t queue,void *_Nullable context,dispatch_function_t work);
說明:
將應(yīng)用程序定義的函數(shù)排入隊(duì)列以便在指定時(shí)間執(zhí)行。
25.
void dispatch_barrier_async(dispatch_queue_tqueue,dispatch_block_tblock);
說明:
提交用于異步執(zhí)行的屏障塊并立即返回钟病。效果類似 dispatch_async萧恕,區(qū)別就是中間多了一個(gè)barrier,barrier顧名思義就是屏障的意思档悠,將隊(duì)列一分為2廊鸥,前面的代碼執(zhí)行完才能執(zhí)行。
實(shí)例:
dispatch_queue_t concurrent_queue = dispatch_queue_create("concurrent_queue", DISPATCH_QUEUE_CONCURRENT);
? ? dispatch_async(concurrent_queue, ^(){
? ? ? ? NSLog(@"task-1--%@",[NSThread currentThread]);
? ? });
? ? dispatch_async(concurrent_queue, ^(){
? ? ? ? NSLog(@"task-2--%@",[NSThread currentThread]);
? ? });
? ? dispatch_async(concurrent_queue, ^(){
? ? ? ? NSLog(@"task-3--%@",[NSThread currentThread]);
? ? });
? ? dispatch_barrier_sync(concurrent_queue, ^(){
? ? ? ? NSLog(@"dispatch_barrier_async--%@",[NSThread currentThread]);
? ? });
? ? dispatch_async(concurrent_queue, ^(){
? ? ? ? NSLog(@"task-4--%@",[NSThread currentThread]);
? ? });
? ? dispatch_async(concurrent_queue, ^(){
? ? ? ? NSLog(@"task-5--%@",[NSThread currentThread]);
? ? });
? ? dispatch_async(concurrent_queue, ^(){
? ? ? ? NSLog(@"task-6--%@",[NSThread currentThread]);
? ? });
task-1/2/3 和 task-4/5/6 分別并發(fā)執(zhí)行辖所,dispatch_barrier_async就像一座屏障惰说,把1/2/3和4/5/6分隔開來,
26.
void dispatch_barrier_async_f(dispatch_queue_tqueue,void *_Nullable context,dispatch_function_t work);
說明:
提交用于異步執(zhí)行的屏障函數(shù)并立即返回缘回。
27.
void dispatch_barrier_sync(dispatch_queue_t queue,DISPATCH_NOESCAPE dispatch_block_t block);
說明:
提交屏障塊對象以供執(zhí)行吆视,并等待該塊完成。
28.
void dispatch_barrier_sync_f(dispatch_queue_t queue,void *_Nullable context,dispatch_function_t work);
說明:
提交屏障功能以執(zhí)行并等待該功能完成酥宴。
29.
void dispatch_queue_set_specific(dispatch_queue_t queue,const void * key,void *_Nullable context,dispatch_function_t_Nullable destructor);
說明:
設(shè)置指定調(diào)度隊(duì)列的鍵/值數(shù)據(jù)啦吧。
30.
void *_Nullable dispatch_queue_get_specific(dispatch_queue_t queue,const void * key);
說明:
獲取與指定的調(diào)度隊(duì)列關(guān)聯(lián)的鍵的值。
31.
void *_Nullable dispatch_get_specific(const void * key);
說明:
返回與當(dāng)前調(diào)度隊(duì)列關(guān)聯(lián)的鍵的值拙寡。
32.
void dispatch_assert_queue(dispatch_queue_t queue)DISPATCH_ALIAS_V2(dispatch_assert_queue);
33.
void dispatch_assert_queue_barrier(dispatch_queue_t queue);
34.
void dispatch_assert_queue_not(dispatch_queue_t queue)DISPATCH_ALIAS_V2(dispatch_assert_queue_not);
十一:semaphore.h
1.
dispatch_semaphore_t dispatch_semaphore_create(long value);
說明:
創(chuàng)建具有初始值的新計(jì)數(shù)信號量授滓。
2.
long dispatch_semaphore_wait(dispatch_semaphore_t dsema,dispatch_time_t timeout);
說明:
等待(遞減)信號量。
3.
long dispatch_semaphore_signal(dispatch_semaphore_t dsema);
說明:
信號或增量肆糕,信號量般堆。
十二:source.h
1.
typedef const struct dispatch_source_type_s * dispatch_source_type_t;
2.
typedef unsigned long dispatch_source_mach_send_flags_t;
3.
typedef unsigned long dispatch_source_memorypressure_flags_t;
4.
typedef unsigned long dispatch_source_proc_flags_t;
5.
typedef unsigned long dispatch_source_vnode_flags_t;
6.
typedef unsigned long dispatch_source_timer_flags_t;
7.
dispatch_source_t dispatch_source_create(dispatch_source_type_ttype,uintptr_t handle,unsigned long mask,dispatch_queue_t _Nullable queue);
說明:
創(chuàng)建新的調(diào)度源以監(jiān)視低級系統(tǒng)對象,并自動(dòng)將處理程序塊提交到調(diào)度隊(duì)列以響應(yīng)事件诚啃。
8.
void dispatch_source_set_event_handler(dispatch_source_t source,dispatch_block_t _Nullable handler);
說明:
為給定的調(diào)度源設(shè)置事件處理程序塊淮摔。
9.
void dispatch_source_set_event_handler_f(dispatch_source_t source,dispatch_function_t _Nullable handler);
說明:
為給定的調(diào)度源設(shè)置事件處理函數(shù)。
10.
void dispatch_source_set_cancel_handler(dispatch_source_t source,dispatch_block_t _Nullable handler);
說明:
為給定的調(diào)度源設(shè)置取消處理程序塊始赎。
11.
void dispatch_source_set_cancel_handler_f(dispatch_source_t source,dispatch_function_t _Nullable handler);
說明:
設(shè)置給定調(diào)度源的取消處理函數(shù)和橙。
12.
void dispatch_source_cancel(dispatch_source_t source);
說明:
異步取消調(diào)度源,防止進(jìn)一步調(diào)用其事件處理程序塊造垛。
13.
long dispatch_source_testcancel(dispatch_source_t source);
說明:
測試給定的調(diào)度源是否已被取消魔招。
14.
uintptr_t dispatch_source_get_handle(dispatch_source_t source);
說明:
返回與指定的調(diào)度源關(guān)聯(lián)的基礎(chǔ)系統(tǒng)句柄。
15.
unsigned long dispatch_source_get_mask(dispatch_source_t source);
說明:
返回調(diào)度源監(jiān)視的事件掩碼筋搏。
16.
unsigned long dispatch_source_get_data(dispatch_source_t source);
說明:
返回調(diào)度源的待處理數(shù)據(jù)仆百。
17.
void dispatch_source_merge_data(dispatch_source_t source,unsigned long value);
說明:
合并數(shù)據(jù)轉(zhuǎn)換成類型的調(diào)度源或和提交其事件處理程序塊到它的目標(biāo)隊(duì)列。
18.
void dispatch_source_set_timer(dispatch_source_t source,dispatch_time_t start,uint64_tinterval,uint64_tleeway);
說明:
設(shè)置計(jì)時(shí)器源的開始時(shí)間奔脐,間隔和余地值俄周。
19.
void dispatch_source_set_registration_handler(dispatch_source_t source,dispatch_block_t _Nullable handler);
說明:
為給定的調(diào)度源設(shè)置注冊處理程序塊吁讨。
20.
void dispatch_source_set_registration_handler_f(dispatch_source_t source,dispatch_function_t _Nullable handler);
說明:
設(shè)置給定調(diào)度源的注冊處理函數(shù)。
十三:time.h
1.
typedef uint64_t dispatch_time_t;
2.
dispatch_time_t dispatch_time(dispatch_time_t when,int64_t delta);
說明:
創(chuàng)建默認(rèn)時(shí)鐘的相對值或修改現(xiàn)有時(shí)鐘
3.
dispatch_time_t dispatch_walltime(const struct timespec *_Nullable when,int64_t delta);
說明:
根據(jù)掛鐘使用絕對時(shí)間創(chuàng)建