Sync Vs Async Queue In Ios
Solution 1:
Since you're using the same queue for both works, the second async block won't start executing until the first block ends. It doesn't matter if it's asynchronous or serial.
You'll see the true difference between .async
and .sync
if you add a print statement between both queues. Like this:
queue.async {
for_in1...100 {
self.smile()
}
}
print("Finished printing smiles")
queue.async {
for_in1...100 {
self.love()
}
}
The previous code will probably print "Finished printing smiles" even before it has started printing smiles! That's because async work returns instantly and keeps executing code.
And let's see what happens if you change the queues with synchronous queues:
queue.sync {
for_in1...100 {
self.smile()
}
}
print("Finished printing smiles")
queue.sync {
for_in1...100 {
self.love()
}
}
Yup. Now the sync queue waits before the closure completes before carrying on. So, you'll get 100 smiles, and then the "Finished printing smiles".
If you want to achieve concurrency, that's it, two blocks of code executing simultaneously (but not at exactly the same time, because that would be parallelism), you'll have to use two different queues, or specify the .concurrent
parameter in the queue configuration:
overridefuncviewDidLoad() {
let queue =DispatchQueue(label: "SerialQueue")
let queue2 =DispatchQueue(label: "AnotherQueue")
queue.async {
for_in1...100 {
self.smile()
}
}
print("Finished printing smiles")
queue2.async {
for_in1...100 {
self.love()
}
}
}
As you'll see, the order here is chaotic and will vary between executions. That's because both queues are running at the same time.
Another equivalent to this code would be:
let queue =DispatchQueue(label: "ConcurrentQueue", attributes: .concurrent)
queue.async {
for_in1...100 {
self.smile()
}
}
print("Finished printing smiles")
queue.async {
for_in1...100 {
self.love()
}
}
Solution 2:
@loufranco and @Roberto has answered in detailed way.
You can also achieve this on a single OperationQueue
adding different BlockOperation
on it.
As if you will see in this scenario:
letqueue = OperationQueue()
letoperation1 = BlockOperation {
for_in1...5 {
smile()
}
}
print("Done")
letoperation2 = BlockOperation {
for_in1...5 {
love()
}
}
queue.addOperation (operation1)
queue.addOperation (operation2)
The output is :
And if you will add operation2 dependency on operation1:
operation2.addDependency(operation1)
queue.addOperation (operation1)
queue.addOperation (operation1)
The Output is :
Solution 3:
When you use sync, it executes in the queue’s thread, but sync doesn’t return until it is done. async returns immediately.
Because you have a serial queue, the prints are the same, but the calling function can return before the queue has done the printing. If it were sync, the calling function would be waiting for the prints to finish.
There is no async/await concept in Swift yet. That is not what is happening here (or in the sync case)
If you want to see the difference, put sleeps in the blocks and prints outside the queue calls.
Post a Comment for "Sync Vs Async Queue In Ios"