Multithreading in iOS-Part 2/4

(DispatchWorkItem, DispatchGroup, DispatchBarrier, DispatchSemaphore, DispatchSources)


How can we cancel a work item:-

  1. If the task has not yet started on the queue, it will be removed.
  2. If the task is currently executing, the isCancelled property will be set to true


  1. Create a new dispatch group.
  2. Call enter() to manually notify the group that a task has started. You must balance out the number of enter() calls with the number of leave() calls else your app will crash.
  3. You call wait() to block the current thread while waiting for tasks completion. You can use wait(timeout:) to specify a timeout and bail out on waiting after a specified time.
  4. Instead ofwait() or wait(timeout:) you notify the group that this work is done.
  5. At this point, you are guaranteed that all tasks have either completed or timed out. You then make a call back to the main queue to run your completion closure.
1. wait with time out 2. output

In which situations you can use DispatchGroup?

  • When you need to run two distinct network calls. Only after they both have returned you have the necessary data to parse the responses.
  • When you need to add animation as well as some network api call or database update. After these two work you need to show message and many more situations like this.
different syntax used here we can follow anything to execute code

How can you Delay the task Execution:-

  • microseconds
  • milliseconds
  • nanoseconds

How can we get Current Queue Name?


with dispatchBarrier
  • Custom Serial Queue: A bad choice here. barriers won’t do anything helpful since a serial queue executes one operation at a time anyway.
  • Global Concurrent Queue: Use caution here. This probably isn’t the best idea because other systems might be using the queues and it will block that execution until barrier code completes its execution.
  • Custom Concurrent Queue: This is a great choice for atomic or critical areas of code. Anything you’re setting or instantiating that needs to be thread-safe is a great candidate for a barrier.


  • call wait() each time before using the shared resource. Here we are asking semaphore whether shared resource is available or not
  • call signal() each time after using the shared resource. Signalling the semaphore that we are done interacting with the shared resource.
  • Decrement semaphore counter by 1.
  • If the resulting value is less than zero, thread is blocked and will go into waiting state.
  • If the resulting value is equal or bigger than zero, code will get executed without waiting.
  • Increment semaphore counter by 1.
  • If the previous value was less than zero, this function unblock the thread currently waiting in the thread queue.
  • If the previous value is equal or bigger than zero, it means thread queue is empty that means no one is waiting.
without semaphore. when run this code each and every time we get different result
with semaphore- we always get result as [0, 1, 2, 3,4, 5, 6, 7, 8, 9, 10]


  • Timer Dispatch Sources (DispatchSourceTimer): Used to generate periodic notifications.
  • Signal Dispatch Sources (DispatchSourceSignal): Used to handle UNIX signals.
  • Memory Dispatch Sources (DispatchSourceMemoryPressure): Used to register for notifications related to the memory usage status .
  • Descriptor Dispatch Sources (DispatchSourceFileSystemObject, DispatchSourceRead, DispatchSourceWrite): Descriptor sources sends notifications related to a various file- and socket-based operations, such as:
  1. signal when data is available for reading
  2. signal when it is possible to write data
  3. files delete, move, or rename
  4. files meta information change
  • Process dispatch sources (DispatchSourceProcess): Used to monitor external process for some events related to their execution state. Process-related events, such as
  1. a process exits
  2. a process issues a fork or exec type of call
  3. a signal is delivered to the process.

Let’s see some use cases:

  • Timer runs on main thread which needs main run loop to execute. If you want to execute Timer on background thread, you can’t because Timer requires an active run loop which is not always readily available on background queues.
  • In this situation DispatchSourceTimer could be used. A dispatch timer source, fires an event when the time interval has been completed, which then fires a pre-set callback all on the same queue.




iOS Developer in walmart

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store