So I put 10 tasks in a parallel queue using dispatch_async . They do not block the next task and are processed in order. My user interface is responsive.
for (int i = 0; i < 10; i++) {
dispatch_async(concurrencyQueue, ^() {
NSLog(@"..calling insertion method to insert record %d", i);
dispatch_sync(serialQueue, ^() {
NSLog(@"----------START %d---------", i);
[NSThread sleepForTimeInterval:1.0f];
NSLog(@"--------FINISHED %d--------", i);
});
});
}
For each task, we simulate writing to the database with a “1 second sleep” in a sequential queue via dispatch_sync .
I always thought that dispatch_sync blocks everyone and synchronizes their tasks, because this is how it behaves when I use it individually. However, in this situation, it does not block the main thread. Instead, it works beautifully in the background as I want.
Is it because any thread is associated with the queue?
, dispatch_async .
dispatch_sync , . , dispatch_sync , .
?
!