当前位置: 移动技术网 > IT编程>移动开发>IOS > 详解iOS多线程GCD的使用

详解iOS多线程GCD的使用

2019年07月24日  | 移动技术网IT编程  | 我要评论

我的爱像订书机,喝女王的尿,沃联系

grand central dispatch(gcd)是异步执行任务的技术之一

dispatch queue分成以下三种:

1)运行在主线程的main queue,通过dispatch_get_main_queue获取。

/*!
* @function dispatch_get_main_queue
*
* @abstract
* returns the default queue that is bound to the main thread.
*
* @discussion
* in order to invoke blocks submitted to the main queue, the application must
* call dispatch_main(), nsapplicationmain(), or use a cfrunloop on the main
* thread.
*
* @result
* returns the main queue. this queue is created automatically on behalf of
* the main thread before main() is called.
*/
__osx_available_starting(__mac_10_6,__iphone_4_0)
dispatch_export struct dispatch_queue_s _dispatch_main_q;
#define dispatch_get_main_queue() \
dispatch_global_object(dispatch_queue_t, _dispatch_main_q)

可以看出,dispatch_get_main_queue也是一种dispatch_queue_t。

2)并行队列global dispatch queue,通过dispatch_get_global_queue获取,由系统创建三个不同优先级的dispatch queue。并行队列的执行顺序与其加入队列的顺序相同。

3)串行队列serial queues一般用于按顺序同步访问,可创建任意数量的串行队列,各个串行队列之间是并发的。

当想要任务按照某一个特定的顺序执行时,串行队列是很有用的。串行队列在同一个时间只执行一个任务。我们可以使用串行队列代替锁去保护共享的数据。和锁不同,一个串行队列可以保证任务在一个可预知的顺序下执行。

serial queues通过dispatch_queue_create创建,可以使用函数dispatch_retain和dispatch_release去增加或者减少引用计数。

gcd的用法:

 // 后台执行:
 dispatch_async(dispatch_get_global_queue(0, 0), ^{
  // something
 });

 // 主线程执行:
 dispatch_async(dispatch_get_main_queue(), ^{
  // something
 });

 // 一次性执行:
 static dispatch_once_t oncetoken;
 dispatch_once(&oncetoken, ^{
  // code to be executed once
 });

 // 延迟2秒执行:
 double delayinseconds = 2.0;
 dispatch_time_t poptime = dispatch_time(dispatch_time_now, delayinseconds * nsec_per_sec);
 dispatch_after(poptime, dispatch_get_main_queue(), ^(void){
  // code to be executed on the main queue after delay
 });

 // 自定义dispatch_queue_t
 dispatch_queue_t urls_queue = dispatch_queue_create("blog.devtang.com", null);
 dispatch_async(urls_queue, ^{ 
   // your code 
 });
 dispatch_release(urls_queue);

 // 合并汇总结果
 dispatch_group_t group = dispatch_group_create();
 dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
  // 并行执行的线程一
 });
 dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
  // 并行执行的线程二
 });
 dispatch_group_notify(group, dispatch_get_global_queue(0,0), ^{
  // 汇总结果
 });

一个应用gcd的例子:

 dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{
  nsurl * url = [nsurl urlwithstring:@"http://www.baidu.com"];
  nserror * error;
  nsstring * data = [nsstring stringwithcontentsofurl:url encoding:nsutf8stringencoding error:&error];
  if (data != nil) {
   dispatch_async(dispatch_get_main_queue(), ^{
    nslog(@"call back, the data is: %@", data);
   });
  } else {
   nslog(@"error when download:%@", error);
  }
 });

gcd的另一个用处是可以让程序在后台较长久的运行。

在没有使用gcd时,当app被按home键退出后,app仅有最多5秒钟的时候做一些保存或清理资源的工作。但是在使用gcd后,app最多有10分钟的时间在后台长久运行。这个时间可以用来做清理本地缓存,发送统计数据等工作。

让程序在后台长久运行的示例代码如下:

// appdelegate.h文件
@property (assign, nonatomic) uibackgroundtaskidentifier backgroundupdatetask;

// appdelegate.m文件
- (void)applicationdidenterbackground:(uiapplication *)application
{
 [self beingbackgroundupdatetask];
 // 在这里加上你需要长久运行的代码
 [self endbackgroundupdatetask];
}

- (void)beingbackgroundupdatetask
{
 self.backgroundupdatetask = [[uiapplication sharedapplication] beginbackgroundtaskwithexpirationhandler:^{
  [self endbackgroundupdatetask];
 }];
}

- (void)endbackgroundupdatetask
{
 [[uiapplication sharedapplication] endbackgroundtask: self.backgroundupdatetask];
 self.backgroundupdatetask = uibackgroundtaskinvalid;
}

以上内容是小编给大家介绍的ios中gcd的使用,希望对大家有所帮助!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网