dispatch_barrier_sync / dispatch_barrier_async區(qū)別
dispatch_barrier_sync
Submits a barrier block object for execution and waits until that block completes.
提交一個柵欄函數(shù)在執(zhí)行中,它會等待柵欄函數(shù)執(zhí)行完
dispatch_barrier_async
Submits a barrier block for asynchronous execution and returns immediately.提交一個柵欄函數(shù)在異步執(zhí)行中,它會立馬返回
- dispatch_barrier_sync 需要等待柵欄執(zhí)行完才會執(zhí)行柵欄后面的任務(wù)
- dispatch_barrier_async 無需等待柵欄執(zhí)行完,會繼續(xù)往下走
共同點:
1洲赵、都會等待在它前面插入隊列的任務(wù)(1躁垛、2、3)先執(zhí)行完
2、都會等待他們自己的任務(wù)(barrier)執(zhí)行完再執(zhí)行后面的任務(wù)(4、5、6)
不共同點:
1、dispatch_barrier_sync需要等待自己的任務(wù)(barrier)結(jié)束之后,才會繼續(xù)執(zhí)行寫在barrier后面的任務(wù)(4刻炒、5、6)自沧,然后執(zhí)行后面的任務(wù)
2坟奥、dispatch_barrier_async將自己的任務(wù)(barrier)插入到queue之后,不會等待自己的任務(wù)結(jié)束拇厢,它會繼續(xù)把后面的任務(wù)(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);
//異步函數(shù) 無序執(zhí)行
NSLog(@"%@ >>>>> barrier 前面 1 ", [NSThread currentThread]);
dispatch_async(queue, ^{
sleep(5);
NSLog(@"%@ >>>>> 任務(wù) 1 ", [NSThread currentThread]);
});
NSLog(@"%@ >>>>> barrier 前面 2 ", [NSThread currentThread]);
dispatch_async(queue, ^{
sleep(3);
NSLog(@"%@ >>>>> 任務(wù) 2 ", [NSThread currentThread]);
});
NSLog(@"%@ >>>>> barrier 前面 3 ", [NSThread currentThread]);
dispatch_async(queue, ^{
sleep(1);
NSLog(@"%@ >>>>> 任務(wù) 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(@"%@ >>>>> 任務(wù) 4 ", [NSThread currentThread]);
});
NSLog(@"%@ >>>>> barrier 后面 5 ", [NSThread currentThread]);
dispatch_async(queue, ^{
sleep(10);
NSLog(@"%@ >>>>> 任務(wù) 5 ", [NSThread currentThread]);
});
NSLog(@"%@ >>>>> barrier 后面 6 ", [NSThread currentThread]);
dispatch_async(queue, ^{
NSLog(@"%@ >>>>> 任務(wù) 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 區(qū)別
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]];
//加或不加“/” 結(jié)果都一樣 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 是用于處理一般文件或文件夾路徑孝偎,路徑字符串內(nèi)不允許有兩個連續(xù)的"http://“存在访敌;
- NSURL 的URLByAppendingPathComponent 則是明確用于對 request Url 進行 component 拼接的辕录,拼接的 component 中加不加”/“都一樣眉尸,這個和 string 的 拼接 component 同理
參考點1