今天查找的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
{
}