GCD实战一:使用串行队列实现简单的预加载
来源:互联网 发布:虚拟聊天软件下载 编辑:程序博客网 时间:2024/05/21 21:41
其主要思路是使用gcd创建串行队列,然后在此队列中先后执行两个任务:1.预加载一个viewController 2.将这个viewController推入
代码如下:
@implementation DWAppDelegate{ dispatch_queue_t _serialQueue; UINavigationController *_navController;}- (dispatch_queue_t)serialQueue{ if (!_serialQueue) { _serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);//创建串行队列 } return _serialQueue;}- (void)prepareViewController{ dispatch_async([self serialQueue], ^{//把block中的任务放入串行队列中执行,这是第一个任务 self.viewController = [[[DWViewController alloc] init] autorelease]; sleep(2);//假装这个viewController创建起来很花时间。。其实view都还没加载,根本不花时间。 NSLog(@"prepared"); });}- (void)goToViewController{ dispatch_async([self serialQueue], ^{//第二个任务,推入viewController NSLog(@"go"); dispatch_async(dispatch_get_main_queue(), ^{//涉及UI更新的操作,放入主线程中 [_navController pushViewController:self.viewController animated:YES]; }); });}- (void)dealloc{ dispatch_release(_serialQueue); [_navController release]; [_window release]; [_viewController release]; [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ [self prepareViewController]; self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. DWViewController *viewController = [[[DWViewController alloc] initWithNibName:@"DWViewController" bundle:nil] autorelease]; viewController.view.backgroundColor = [UIColor blueColor]; _navController = [[UINavigationController alloc] initWithRootViewController:viewController]; self.window.rootViewController = _navController; [self goToViewController]; [self.window makeKeyAndVisible]; return YES;}
工程下载:
gcdtest1.zip (23.9 KB, 393 次)
- GCD实战一:使用串行队列实现简单的预加载
- GCD实战一:使用串行队列实现简单的预加载
- GCD实战一:使用串行队列实现简单的预加载
- GCD实战一:使用串行队列实现简单的预加载
- GCD实战一:使用串行队列实现简单的预加载
- GCD实战一:使用串行队列实现简单的预加载
- GCD实战一:使用串行队列实现简单的预加载
- GCD实战一:使用串行队列实现简单的预加载
- GCD实战一:使用串行队列实现简单的预加载
- iOS学习笔记10(5)—GCD实战一:使用串行队列实现简单的预加载
- iOS系统GCD学习(11):使用串行队列实现简单的预加载
- GCD 的使用 并行串行 队列
- GCD使用(六)串行队列的构造
- GCD的串行队列和并行队列
- 多线程GCD的详细讲解 任务和队列(串行队列,并发队列) (一)
- iOS-GCD的串行队列和并行队列的任务及实现
- GCD串行队列
- GCD 串行队列,并发队列和主队列的整理
- LPC2387看门狗功能
- GCD介绍(一): 基本概念和Dispatch Queue
- GCD介绍(二): 多核心的性能
- Windows Embedded Compact 2013 开发平台与目标硬件之间的几种连接方式
- GCD介绍(三): Dispatch Sources
- GCD实战一:使用串行队列实现简单的预加载
- 档案文件properties的使用
- GCD实战2:资源竞争
- WPF第三篇——listbox选择后显示隐藏菜单
- POJ 1251 (2013.9.21周赛H题:MST最小生成树prim和kruskal算法)
- linux基础命令
- paip. c++ doxygen 文档工具的使用以及跟QT CREATOR的集成
- Block教程系列
- 十、android的ContentProvider对外共享数据