GCD (Grand Central Dispatch) - Imtiaz211/interviews GitHub Wiki

Grand Central Dispatch?

GCD is an extremely important feature introduced in iOS 4.0, OS X 10.6 and later versions. Barrier and semaphor are used for pause, and play. groupworkItem used for cancel the API call. GCD works with blocks, which are blocks of code that you need to execute in the background, low, default and high priority queue, in a serial or concurrent order.

What is Concurrency?

  1. Concurrency is dividing up the execution paths of your program so that they are possibly running at the same time. The common terminology: process, thread, multithreading, and others. Terminology;
  2. Process, An instance of an executing app
  3. Thread, Path of execution for code
  4. Multithreading, Multiple threads or multiple paths of execution running at the same time.
  5. Concurrency, Execute multiple tasks at the same time in a scalable manner.
  6. Queues, Queues are lightweight data structures that manage objects in the order of First-in, First-out (FIFO).

NSOperationQueue?

The NSOperationQueue class regulates the execution of a set of NSOperationObjects. An OperationQueue is generally used to perform some asynchronous operations on a background thread and not to block the main thread.

NSBlockOperation allows you to create an NSOperation from one or more closures. NSBlockOperations can have multiple blocks that run concurrently.

Main dispatch queue?

Available serial queue that executes tasks on the application's main thread. It is usually called from a background queue when some background processing has finished and the user interface needs to be updated. A custom dispatch queue that schedules tasks for serial execution on an arbitrary thread.

Difference between GCD and Operation?

  1. GCD - methods or chunks of code that need to be executed, GCD is a fitting choice. - GCD is a low-level C-based API that interacts directly with the Unix level of the system. - Both NSOperationsQueue and GCD allow executing heavy computation task in the background on separate threads by freeing the UI Application Main Tread.
  1. OperationQueue - If you’re working with objects that need to encapsulate data and functionality then you’re more likely to utilize Operations. - NSOperation and NSOperationQueue are high-level Objective-C classes. - Instances of NSOperation need to be allocated before they can be used and deallocated when they are no longer needed. Even though this is a highly optimized process - NSOperation slightly shower than GCD.
  1. Dependencies The NSOperation API provides support for dependencies. This is a powerful concept that enables developers to execute tasks in a specific order. An operation is ready when every dependency has finished executing.
  2. Observable The NSOperation and NSOperationQueue classes have a number of properties that can be observed, using KVO (Key Value Observing). This is another important benefit if you want to monitor the state of an operation or operation queue. Pause, Cancel, Resume Operations can be paused, resumed, and cancelled. Once you dispatch a task using Grand Central Dispatch, you no longer have control or insight into the execution of that task. The NSOperation API is more flexible in that respect, giving the developer control over the operation's life cycle.

What is a Serial Queue?

A Serial queue allows us to perform only one task at a time, no matter the way of execution, i.e. Synchronous or Asynchronous. All the queues need to wait for the completion of the previous queue. By default, DispatchQueue is a serial queue.

What is a Concurrent Queue?

A concurrent queue helps us execute multiple tasks at the same time, but there’s a limit to the number of tasks depending on the system. If used incautiously, it can cause Thread explosion and Deadlock.

Quality of Service?

After an app kicks-off and starts a runloop on the main thread, one can begin taking advantage of QoS. QoS breaks out priorities into four different groups. Each one corresponds to common tasks one might find themselves coding in their iOS endeavors.

  1. User Interactive: Work that happens on the main thread, such as animations or drawing operations. highest periority
  2. User Initiated: The user initiates these asynchronous tasks from the UI, Work that the user kicks-off and should yield immediate results. This work must be completed for the user to continue.
  3. Utility: Work that may take a bit and doesn’t need to finish right away. Analogous to progress bars and importing data.
  4. Background: This work isn’t visible to the user. Backups, syncs, indexing, etc. lowest periority
  5. Default: this quality of service periority falls between User Initiated or Utility
  6. Unspecified: Thread can have unspecified QoS.

At launch time which queue use system?

When the app launches, it will be on the main thread or the UI thread.

What is Dispatch Group?

If we need to wait on a couple of asynchronous or synchronous operations before proceeding, we can use DispatchGroup.

Global dispatch queue?

Tasks in concurrent queue executed concurrently [background threads]. Tasks are still started in the order that they were added to the queue.

Enlist the methods to achieve concurrency in iOS?

  1. Threads.
  2. Dispatch queues.
  3. Operation Queues.

What is synchronous and asynchronous request?

In synchronous requests the main thread gets blocked and control will not get back to the user till that request gets executed. Asynchronous control gets back to the user even if a request is getting executed.

What is NSThread?

NSThread when you want a unit of computational work done without necessarily either waiting for other units to finish, or holding up other computational work. And don’t use it in UI Elements. All UI Elements happened on the Main thread. It creates a new low level thread which can be started by calling like, [myThread start];

Static and Dynamic Dispatch ?

  1. Static Dispatch (or Direct Dispatch) is supported by both value types and reference types. they are pretty fast when compared to Dynamic dispatch as the compiler is able to locate where the instructions are, at compile time. So, when the function is called, the compiler jumps directly to the memory address of the function to perform the operation. we need to make use of final and static as both of them ensures that the class and method cannot be overridden.

Dynamic Dispatch is supported only by reference types. dynamic dispatch, in short, we need inheritance and our value types do not support inheritance. The implementationis chosen at runtime instead of compile-time, which adds some overhead. it allows polymorphism to exist.

Semaphore

  1. Semaphore is part of dispatch framework hence it is begin with word dispatch
  2. DispatchWorkItem()
  3. DispatchSemaphore()
  4. DispatchGroup()
  5. Semaphore is a object to control the access of shared resources"

dispatch_once()?

dispatch_once() is absolutely synchronous. Not all GCD methods do things asynchronously (case in point, dispatch_sync() is synchronous). The benefit of dispatch_once() over this is that it's faster. It's also semantically cleaner, because it also protects you from multiple threads doing alloc init of your shared Instance--if they all try at the same exact time.It won't allow two instances to be created. The entire idea of dispatch_once() is "perform something once and only once"