IOS-usr/include-dispatch-queue.h
// 后台执行:dispatch_async(dispatch_get_global_queue(0, 0), ^{ // something });
// 主线程执行:dispatch_async(dispatch_get_main_queue(), ^{ // something });
// 一次性执行:static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ // code to be executed once });
//
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL * url = [NSURL URLWithString:@"http://www.baidu.com"];
NSError * error;
NSString * data = [NSString stringWithContentsOfURL:url encoding: NSUTF8StringEncoding error:&error];
if (data != nil)
{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"call back, the data is: %@", data);
});
}
else
{
NSLog(@"error when download:%@", error);
}
});
//例如dispatch_sync与dispatch_async的区别等。以及如何创建串行并行队列等。
//还有可能会问到block有关的问题,如为何会copy,为何会循环引用并如何避免。
非ARC:
__block CurrentViewController* blockSelf = self;
brush.getCardInfo=^(NSDictionary *info){
[blockSelf test];
};
通过这个方式,告诉block这个变量的引用计数不要+1。
如果你使用的是ARC,那么就应该改成下面这样:
__weak CurrentViewController* blockSelf = self;
brush.getCardInfo=^(NSDictionary *info){
[blockSelf test];
};
//1)运行在主线程的Main queue,通过dispatch_get_main_queue获取。
//2)并行队列global dispatch queue,通过dispatch_get_global_queue获取,由系统创建三个不同优先级的dispatch queue。并行队列的执行顺序与其加入队列的顺序相同。
//3)串行队列serial queues一般用于按顺序同步访问,可创建任意数量的串行队列,各个串行队列之间是并发的。