Pages

Introduction to Quality of Service Classes(QoS)

        Quality of Service Classes(QoS) is an API with the help of which you can tell the Operating System what kind of work you are doing with what priority. This API is used in situation when you run out of CPU s to perform tasks and can't set priority of tasks i.e. in which order they will execute. Quality of Service Classes(QoS) are nothing more than flags which you will be feeding to dispatch objects to communicate to OS. Different four types of QoS are stated below:

1.) User Interactive(UI)
2.) User Initiative(IN)
3.) Utility(UT)
4.) Background(BG)

        Each of these classes performs different functionality and have a particular priority and are used for resource control to effectively execute your code. Resource control means managing CPU Scheduling, I/O priority, CPU throughput , CPU efficiency etc.

dispatch_once() in extream depth

        In this post I will be discussing all about dispatch_once(), why to use it?, when to use it?, what exactly it does? and what is happening under the hood?
        dispatch_once() behaves as it is specified by it's name, whatever it does it is once and only once. And that's what makes it very interesting API of all in GCD. It's syntactical form in  implemented below, it takes two argument, first is flag that takes care of the once thing and second is the task that you want to perform only once.

         static var flag: dispatch_once_t = 0
         dispatch_once(&static.flag) {
                    //perform some task here.
         } 

        It is a very best and suitable API for lazily initializing shared state of any type kind of dictionary, array or anything. Also it is much faster API than locks which do same thing.
        Now a million dollar thing this API is kind of a swordsman without it's sword in Single-threaded environment and can be replace by simple if-else block. We use it and Apple documentation recommends it to use in multi-threaded environment because of it's ability of being Thread-Safe, means if multiple threads are executing same shared code then it will perform a kind of lock on block/task thread is executing and won't leave until thread unlock it, meanwhile all other threads have to wail for that lock to relinquished.
 

GCD's dispatch_once() and Singleton Class

        Singleton is an object which is restricted to be the only instance of its class, I believe you guys are familiar with this definition. In this tutorial you will learn why and not to use it. What are it's advantages and disadvantages. And what happens when you use it with GCD's dispatch_once() function.
        
Advantages of using Singletons: 

1.) A singleton object is created only once and once created it will available through out you program/app. And it will let you access all objects defined in this class as they are global from any other class.
2.) If any class represents an entity which inherently Singular then having a single instance of a class is most appropriate.
3.) Singletons are most appropriate in situations when you have a lot of common functionality that will be used frequently, you can define that functionality at only one place.
4.) Resources allocated in Singleton are not allocated until they are needed means they are initialized Lazily. 

Running a task in background with dispatch_async()

        In the first part of this series I have introduced the basics of Grand Central Dispatch API, it's uses and its advantages. In this tutorial I will be demonstrating how to use it in your real app. I will be showing example of two functions dispatch_sync() and dispatch_async(). 
        
1.) dispatch_sync() is used for synchronization purpose in your application. It blocks thread on which you use it until block you submit to this function will successfully complete its execution. This function performs lock on the thread you use it on.

Here are two implementations of dispatch_sync():

dispatch_sync(dispatch_get_main_queue()) {
               //your task to execute
                print("Synchronous task which blocks main thread.")


        Above function is just basic demonstrate how dispatch_sync() works. Here what is happening is, you passed main queue as an argument to dispatch_sync() function which is like you are executing this function on main thread. So this will block your main thread until the task you submitted will complete it's execution. It means this function will not return immediately. Of course there are a lot more ways to use this function, on different threads that is. for example:

Grand Central Dispatch(GCD) Basics

What Is Grand Central Dispatch?

        GCD is basically all about Blocks and Queues. GCD is a very low level C based Abstract Programming Interface(API) with the help of which we can perform concurrent programming. It's basic functionality is pretty much like NSOperationQueue, by using GCD API developers can divide their program into individual tasks which are then submitted to queues which can execute these tasks concurrently or serially as specified. GCD is lower level and higher performance API than NSOperationQueue, and again it is not a part of the Cocoa frameworks.

        Apart from the facility of parallel execution of code(individual tasks), GCD also provides a facility known as event handling system. In event handling system Handlers can be set up to respond to events on file descriptors, mach ports, and processes, to timers and signals which is pretty much like a facility of signal handling in C and to user-generated events. These handlers or functions which handle these events are executed through the GCD facilities for concurrent execution.

Learning Key Value Observing with an example

Key value observing is a feature in Swift that depends on dynamically dispatched methods. You might ask what thats means, well dynamically dispatched methods are those instance methods of an object which are run by querying the related object. For further explanation I recommend that you refer to my post "Key Value Observing In Swift". Let us learn it with an example in which we will have two text fields first one is for entering name, second one if for entering age && a button in storyboard. Now create two outlets for the two textfields && an action function fro button below shown picture is showing this arrangement: