GCD
2016-03-23 22:41 灯泡虫 阅读(132) 评论(0) 编辑 收藏 举报总是习惯把知识记录在本地,是时候改变一下存储知识的方式了,丰富自己,帮助他人!
1.GCD 的一般用法
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//耗时操作。。
dispatch_async(dispatch_get_main_queue(), ^{
//通知主线程刷新
})
});
2.用GCD做定时器
@property (nonatomic) dispatch_source_t timerSource;
- (void)updateWithInte:(NSInteger) time{
NSTimeInterval period = time; //设置时间间隔
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
_timerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(_timerSource, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); //每秒执行
dispatch_source_set_event_handler(_timerSource, ^{
NSLog(@"nihao");
});
dispatch_resume(_timerSource);
}