今天查找的GCD的一个博客:http://blog.devtang.com/blog/2012/02/22/use-gcd/
在主线程设置一个timer,定时执行某项操作。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self startATimer];
}
- (void)startATimer{
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(getNewSession:) userInfo:nil repeats:YES];//这里还是主线程的东西
}
- (void)getNewSession:(NSTimer *)theTimer{
[self performSelectorInBackground:@selector(didNewSession) withObject:nil];
}
- (void)didNewSession
{
//这里就设置了一个新的线程执行了
@autoreleasepool {
DLog(@"timer get new seesion");
//若是要更新主线程的ui.则执行这个
[self performSelectorOnMainThread:@selector(refreshUI) withObject:nil waitUntilDone:YES];
}
}
- (void)refreshUI
{
}
本文介绍了一种在iOS应用中利用Grand Central Dispatch (GCD)进行后台线程处理的方法,通过在主线程中设置定时器并调度后台任务,有效避免了UI阻塞,提升了用户体验。
1636

被折叠的 条评论
为什么被折叠?



