dispatch源


[objc]  view plain copy
  1. 原文地址  http://www.cnblogs.com/sunfrog/p/3243230.html  
  2.   
  3. dispatch源(dispatch source)和RunLoop源概念上有些类似的地方,而且使用起来更简单。要很好地理解dispatch源,其实把它看成一种特别的生产消费模式。dispatch源好比生产的数据,当有新数据时,会自动在dispatch指定的队列(即消费队列)上运行相应地block,生产和消费同步是dispatch源会自动管理的。  
  4. dispatch源的使用基本为以下步骤:  
  5.   
  6. 1. dispatch_source_t source = dispatch_source_create(dispatch_source_type, handler, mask, dispatch_queue); //创建dispatch源,这里使用加法来合并dispatch源数据,最后一个参数是指定dispatch队列  
  7.   
  8. 2. dispatch_source_set_event_handler(source, ^{ //设置响应dispatch源事件的block,在dispatch源指定的队列上运行  
  9.   
  10.   //可以通过dispatch_source_get_data(source)来得到dispatch源数据  
  11.   
  12. });  
  13.   
  14. 3. dispatch_resume(source); //dispatch源创建后处于suspend状态,所以需要启动dispatch源  
  15.   
  16. 4. dispatch_source_merge_data(source, value); //合并dispatch源数据,在dispatch源的block中,dispatch_source_get_data(source)就会得到value。  
  17.   
  18. 是不是很简单?而且完全不必编写同步的代码。比如网络请求数据的模式,就可以这样来写:  
  19.   
  20.     dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_DATA_ADD, 00, dispatch_get_global_queue(00));  
  21.   
  22.     dispatch_source_set_event_handler(source, ^{  
  23.   
  24.         dispatch_sync(dispatch_get_main_queue(), ^{  
  25.   
  26.     //更新UI  
  27.   
  28.         });  
  29.   
  30.     });  
  31.   
  32.     dispatch_resume(source);  
  33.   
  34.     dispatch_async(dispatch_get_global_queue(00), ^{  
  35.   
  36.    //网络请求  
  37.   
  38.         dispatch_source_merge_data(source, 1); //通知队列  
  39.   
  40.     });  
  41.   
  42. dispatch源还支持其它一些系统源,包括定时器、监控文件的读写、监控文件系统、监控信号或进程等,基本上调用的方式原理和上面相同,只是有可能是系统自动触发事件。比如dispatch定时器:  
  43.   
  44. dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 00, queue);  
  45.   
  46. dispatch_source_set_timer(timer, dispatch_walltime(NULL0), 10*NSEC_PER_SEC, 1*NSEC_PER_SEC); //每10秒触发timer,误差1秒  
  47.   
  48. dispatch_source_set_event_handler(timer, ^{  
  49.   
  50.   //定时处理  
  51.   
  52. });  
  53.   
  54. dispatch_resume(timer);  
  55.   
  56. 其它情况的dispatch源就不再一一举例,可参看官网有具体文档: https://developer.apple.com/library/ios/documentation/General/Conceptual/ConcurrencyProgrammingGuide/GCDWorkQueues/GCDWorkQueues.html#//apple_ref/doc/uid/TP40008091-CH103-SW1  
  57.   
  58.    
  59.   
  60. 最后,dispatch源的其它一些函数大致罗列如下:  
  61.   
  62. uintptr_t dispatch_source_get_handle(dispatch_source_t source); //得到dispatch源创建,即调用dispatch_source_create的第二个参数  
  63.   
  64. unsignedlong dispatch_source_get_mask(dispatch_source_t source); //得到dispatch源创建,即调用dispatch_source_create的第三个参数  
  65.   
  66. void dispatch_source_cancel(dispatch_source_t source); //取消dispatch源的事件处理--即不再调用block。如果调用dispatch_suspend只是暂停dispatch源。  
  67.   
  68. long dispatch_source_testcancel(dispatch_source_t source); //检测是否dispatch源被取消,如果返回非0值则表明dispatch源已经被取消  
  69.   
  70. void dispatch_source_set_cancel_handler(dispatch_source_t source, dispatch_block_t cancel_handler); //dispatch源取消时调用的block,一般用于关闭文件或socket等,释放相关资源  
  71.   
  72. void dispatch_source_set_registration_handler(dispatch_source_t source, dispatch_block_t registration_handler); //可用于设置dispatch源启动时调用block,调用完成后即释放这个block。也可在dispatch源运行当中随时调用这个函数。  

你可能感兴趣的:(GCD)