GCD实战一:使用串行队列实现简单的预加载
2012-09-11 10:46
405 查看
转载自:http://www.dreamingwish.com/dream-2012/gcd%E5%AE%9E%E6%88%98%E4%B8%80%EF%BC%9A%E4%BD%BF%E7%94%A8%E4%B8%B2%E8%A1%8C%E9%98%9F%E5%88%97%E5%AE%9E%E7%8E%B0%E7%AE%80%E5%8D%95%E7%9A%84%E9%A2%84%E5%8A%A0%E8%BD%BD.html
其主要思路是使用gcd创建串行队列,然后在此队列中先后执行两个任务:1.预加载一个viewController 2.将这个viewController推入
代码如下:
工程下载:
gcdtest1.zip (23.9
KB, 13 次)
其主要思路是使用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, 13 次)
相关文章推荐
- GCD实战一:使用串行队列实现简单的预加载
- GCD实战一:使用串行队列实现简单的预加载
- GCD实战一:使用串行队列实现简单的预加载
- GCD实战一:使用串行队列实现简单的预加载
- GCD实战一:使用串行队列实现简单的预加载
- GCD实战一:使用串行队列实现简单的预加载
- GCD实战一:使用串行队列实现简单的预加载
- iOS学习笔记10(5)—GCD实战一:使用串行队列实现简单的预加载
- GCD实战练习:使用串行队列实现简单的预加载
- GCD实战一:使用串行队列实现简单的预加载
- GCD实战一:使用串行队列实现简单的预加载
- 深入GCD(四):使用串行队列实现简单的预加载
- iOS系统GCD学习(11):使用串行队列实现简单的预加载
- 浅谈使用java实现阿里云消息队列简单封装
- GCD 的使用 并行串行 队列
- 简单无锁队列的实现和使用
- 使用java实现阿里云消息队列简单封装
- GCD使用 串行并行队列 与 同步异步执行的各种组合 及要点分析
- 简单使用栈实现队列
- GCD的简单使用——同步、异步、串行、并发的具体剖析