bufferoverflow76
bufferoverflow76

Reputation: 797

DispatchQueue sync block vs async block

Assume I have the following DispatchQueue:

let myqueue = DispatchQueue(label: "myqueue")

What is the difference between async block and sync block? Can someone explain what it means?

e.g In the main thread, in the middle execution of the main thread. What will it happen if I call myqueue.async{...} or myqueue.sync{...}

e.g

override func viewDidLoad() {
    super.viewDidLoad()

    myqueue.async{...}

    myqueue.sync{...}
}

Upvotes: 2

Views: 2593

Answers (3)

bufferoverflow76
bufferoverflow76

Reputation: 797

I don't know why my question above is being voted down. as @rmaddy mentioned, the Apple doc for async and sync are unless.

Anyways, I did experiment tested myself for question. So I am going to post my answer here.

  1. What is the difference between async block and sync block? Can someone explain what it means?

Answer: For the code in the async, the line just below the async block will be executed immediately after the dispatchQueue finishes its dispatch job, i.e there are no blocking / waiting for the completion of the async block. On the other hand, for the sync block, the line just below the sync block will be blocked and waiting for the completion of the sync block. Hence, in such case, the sequence of the execution of the code are ensured to be run in one line by one line sequentially.

  1. e.g In the main thread, in the middle execution of the main thread. What will it happen if I call myqueue.async{...} or myqueue.sync{...}?

Answer: myqueue is just a queue. So the elements(dispatched code blocks/jobs/tasks/actions named whatever you like) can be running the main thread or not in the main thread. If we call myqueue.async{...} in the main thread, the dispatched code block will not run in the main thread and will dispatch to another thread for its execution. If we call myqueue.sync{...} in the main thread, the code block will just like a normal code statement so the line just after it will be blocked until the code block is finished and the execution of the them are all happening in the main thread.

For example, since viewDidLoad is always being run in the main thread, if we call myqueue.async{...} inside viewDidLoad, the {...} block will be dispatched to another thread to be executed and the line just below it won't wait for its completion and the execution won't blocked. If we call myqueue.sync{...}, the {...} block will happen in the main thread. So the line just below the block will just wait for completion of the block.

Upvotes: 0

iOS Geek
iOS Geek

Reputation: 4855

When you perform an operation in async mode . main thread will not wait for it to complete it will run parallely to main thread but having advantage that it runs in background thread and sets free your main thread. Like downloading images a bunch of images in Async (Background) thread

And in sync your main thread will wait for that process to complete works in synchronous manner will wait until the process of my queue.sync is not completed

For an example using a collectionView to show multiple images if used Sync first images will be downloaded and then collection view display . until all images are not fetched it won't display and In async images will be downloaded in background and collection view is displayed and as per images are being downloading they will start to display in collection view one by one

Upvotes: 0

Rashwan L
Rashwan L

Reputation: 38833

dispatch_async:

Submits a block for asynchronous execution on a dispatch queue and returns immediately.

dispatch_sync:

Submits a block object for execution on a dispatch queue and waits until that block completes.

dispatch_sync will return after the block is finished meanwhile dispatch_async will return after it is added to the queue and may not be finished.

Example:
You have two tasks, Task1 and Task2.

When you submit a block to run Task1 sync, then Task2 will wait until that is finished before Task2 will run.

When you submit a block to run Task1 async, then Task2 will also run and Task1 can be completed before or after Task2.

Upvotes: 3

Related Questions