iOS GCD introduction

2,451 views

Published on

An introduction to iOS GCD Technology

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,451
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
58
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

iOS GCD introduction

  1. 1. GCD 介绍 <ul><li>Grand Central Dispatch </li></ul>
  2. 2. Agenda <ul><li>GCD 简介 </li></ul><ul><li>应用示例 </li></ul><ul><li>Demo </li></ul><ul><li>其他 </li></ul>
  3. 3. GCD 简介 <ul><li>Grand Central Dispatch (GCD) comprises new language features, runtime libraries, and system enhancements that provide systemic, comprehensive improvements to the support for concurrent code execution on multicore hardware in iOS and Mac OS X. </li></ul><ul><li>简而言之,是 Apple 的提供的 基于操作系统底层 的 多核编程 的 API 库 </li></ul>
  4. 4. 应用示例 – 用户认证和信息获取 <ul><li>假设有个需求如下 </li></ul><ul><li>Step1 :用户认证: </li></ul><ul><li>用户输入用户名密码 </li></ul><ul><li>从网络认证用户信息 </li></ul><ul><li>显示用户认证成功 </li></ul><ul><li>Step2 :用户信息获取: </li></ul><ul><li>如果认证成功,则从网络请求用户信息 </li></ul><ul><li>显示用户信息 </li></ul><ul><li>要求:异步处理,不阻塞界面操作 </li></ul>
  5. 5. 现在想想这个需求的实现方式? - 要创建线程吗? - 要多少个类和方法? - 线程之间的数据如何传递? <ul><li>GCD 可以做到: 优雅   +  高性能 </li></ul>
  6. 6. GCD 实现:一个方法解决问题 <ul><li>d ispatch_asyn(workingQueue, ^{ </li></ul><ul><li>BOOL result = [self sendUserAuthentication(user, password)]; </li></ul><ul><li>dispatch_asyn(dispatch_get_main_queue(), ^{ </li></ul><ul><li>if (result == YES){ </li></ul><ul><li>[self displayAuthenticationSuccess]; </li></ul><ul><li>dispatch_asyn(workingQueue, ^{ </li></ul><ul><li>NSString* user = [self sendGetUserInfo]; </li></ul><ul><li>dispatch_asyn(dispatch_get_main_queue(), ^{ </li></ul><ul><li>[self displayUserInfo]; </li></ul><ul><li>}); </li></ul><ul><li>}); </li></ul><ul><li>} </li></ul><ul><li>else{ </li></ul><ul><li>[self displayAuthenticationFailure]; </li></ul><ul><li>} </li></ul><ul><li>}); </li></ul><ul><li>}); </li></ul>
  7. 7. Demo
  8. 8. GCD 的基础 <ul><li>block programming </li></ul><ul><li>Block objects are a C-level syntactic and runtime feature. They are similar to standard C functions, but in addition to executable code they may also contain variable bindings to automatic (stack) or managed (heap) memory. </li></ul><ul><ul><li>基于 C 语言的可执行的代码块 </li></ul></ul><ul><ul><li>int multiplier = 7; </li></ul></ul><ul><ul><li>int (^myBlock)(int) = ^(int num) { </li></ul></ul><ul><ul><li>return num * multiplier; </li></ul></ul><ul><ul><li>}; </li></ul></ul><ul><ul><li>printf(&quot;%d&quot;, myBlock(3)); // prints &quot;21&quot; </li></ul></ul>
  9. 9. GCD 的主要 API <ul><ul><ul><ul><li>队列创建和销毁 </li></ul></ul></ul></ul><ul><ul><ul><ul><li>disptach_create </li></ul></ul></ul></ul><ul><ul><ul><ul><li>dispatch_release </li></ul></ul></ul></ul><ul><ul><ul><ul><li>分发消息到队列 </li></ul></ul></ul></ul><ul><ul><li>dispatch_asyn </li></ul></ul><ul><ul><li>dispatch_sync </li></ul></ul><ul><ul><ul><ul><li>获得界面主线程队列 </li></ul></ul></ul></ul><ul><ul><li>dispatch_get_main_queue </li></ul></ul>
  10. 10. 其他 <ul><li>尽管代码变得优雅简洁,但是多线程的知识还是必不可少,需要减少资源冲突和注意多线程并发访问 </li></ul><ul><li>有些时候也还是需要使用更高层的 API </li></ul><ul><ul><ul><li>NSThread </li></ul></ul></ul><ul><ul><ul><li>NSOperation & NSOperationQueue </li></ul></ul></ul>

×