dispatch_barrier_sync / dispatch_barrier_async区别
dispatch_barrier_sync
Submits a barrier block object for execution and waits until that block completes.
提交一个栅栏函数在执行中,它会等待栅栏函数执行完
dispatch_barrier_async
Submits a barrier block for asynchronous execution and returns immediately.提交一个栅栏函数在异步执行中,它会立马返回
- dispatch_barrier_sync 需要等待栅栏执行完才会执行栅栏后面的任务
- dispatch_barrier_async 无需等待栅栏执行完,会继续往下走
共同点:
1、都会等待在它前面插入队列的任务(1、2、3)先执行完
2、都会等待他们自己的任务(barrier)执行完再执行后面的任务(4、5、6)
不共同点:
1、dispatch_barrier_sync需要等待自己的任务(barrier)结束之后,才会继续执行写在barrier后面的任务(4、5、6),然后执行后面的任务
2、dispatch_barrier_async将自己的任务(barrier)插入到queue之后,不会等待自己的任务结束,它会继续把后面的任务(4、5、6)插入到queue
参考代码
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
dispatch_async(dispatch_queue_create("mmmmmm", DISPATCH_QUEUE_CONCURRENT), ^{
[self logBarrierOrder];
});
}
- (void)logBarrierOrder {
NSLog(@"%@ >>>>> start ", [NSThread currentThread]);
dispatch_queue_t queue = dispatch_queue_create("test.barrier.queue", DISPATCH_QUEUE_CONCURRENT);
//异步函数 无序执行
NSLog(@"%@ >>>>> barrier 前面 1 ", [NSThread currentThread]);
dispatch_async(queue, ^{
sleep(5);
NSLog(@"%@ >>>>> 任务 1 ", [NSThread currentThread]);
});
NSLog(@"%@ >>>>> barrier 前面 2 ", [NSThread currentThread]);
dispatch_async(queue, ^{
sleep(3);
NSLog(@"%@ >>>>> 任务 2 ", [NSThread currentThread]);
});
NSLog(@"%@ >>>>> barrier 前面 3 ", [NSThread currentThread]);
dispatch_async(queue, ^{
sleep(1);
NSLog(@"%@ >>>>> 任务 3 ", [NSThread currentThread]);
});
NSLog(@"%@ >>>>> barrier 前面 4.1 ", [NSThread currentThread]);
dispatch_barrier_sync(queue, ^{
sleep(7);
NSLog(@"%@ ++++++ barrier ++++++ ", [NSThread currentThread]);
});
NSLog(@"%@ >>>>> barrier 后面 4.2 ", [NSThread currentThread]);
dispatch_async(queue, ^{
NSLog(@"%@ >>>>> 任务 4 ", [NSThread currentThread]);
});
NSLog(@"%@ >>>>> barrier 后面 5 ", [NSThread currentThread]);
dispatch_async(queue, ^{
sleep(10);
NSLog(@"%@ >>>>> 任务 5 ", [NSThread currentThread]);
});
NSLog(@"%@ >>>>> barrier 后面 6 ", [NSThread currentThread]);
dispatch_async(queue, ^{
NSLog(@"%@ >>>>> 任务 6 ", [NSThread currentThread]);
});
NSLog(@"%@ >>>>> end ", [NSThread currentThread]);
}
Test log
dispatch_barrier_sync
dispatch_barrier_sync
dispatch_barrier_async
dispatch_barrier_async
demo
2017年07月22日08:54:33
文件路径 path 与 NSURL 的 path Component appending 区别
NSString or NSURL
NSURL *url = [NSURL URLWithString:@"http://hunlimao.com/wi"];
NSString *urlStr = [NSString stringWithFormat:@"%@",url.absoluteString];
urlStr = [urlStr stringByAppendingPathComponent:[NSString stringWithFormat:@"%d",77]];
NSLog(@"%@",urlStr);
// result is http:/hunlimao.com/wi/77 http后面少了”/“
NSURL *url = [NSURL URLWithString:@"http://hunlimao.com/wi"];
url = [url URLByAppendingPathComponent:[NSString stringWithFormat:@"/%d",77]];
//加或不加“/” 结果都一样 url = [url URLByAppendingPathComponent:[NSString stringWithFormat:@"%d",77]];
NSString *urlStr = [NSString stringWithFormat:@"%@",url.absoluteString];
NSLog(@"%@",urlStr);
// result is http://hunlimao.com/wi/77 是一个正常 url string
- NSString 的 stringByAppendingPathComponent 是用于处理一般文件或文件夹路径,路径字符串内不允许有两个连续的"//“存在;
- NSURL 的URLByAppendingPathComponent 则是明确用于对 request Url 进行 component 拼接的,拼接的 component 中加不加”/“都一样,这个和 string 的 拼接 component 同理
参考点1