Grand Central Dispatch (GCD) Part II
iOS |Grand Centre Dispatch Queue|swift
In Part One We cover serial and concurrent queue and example .On this part we will cover apple more about concurrent queue with argument change and learn how to create global queue Inactive queue,Time Delay, deadlock.
Lets Start from where we left ..
InactiveQueue :
In the attribute parameter have another value initiallyInactive .So we can initially make inactive a queue and when need to active we can make active it .
lets change the attribute value concurrent to initiallyInactive like this :
let newQueue = DispatchQueue(label: “com.concurrent.ekram”, qos: DispatchQoS.background, attributes: .initiallyInactive)
And create class property of DispatchQueue in the outer block of concurrent function :
var inactiveQueue:DispatchQueue!
Now initialise inactive queue with newQueue in the function like this :
inactiveQueue = newQueue
Now newQueue is inactive active state and viewDidAppear don’t know about queue so its need to active manually in viewDidAppear like this :
if let queue = inactiveQueue { queue.activate()
}
Now Code will run and execute queues with serial .. seee the screen and console :
How Can use concurrent when initially inactive :
Simple in attribute parameter use an array with both .initiallyInactive and . concurrent than queue will be initially inactive and concurrent too ..
let newQueue = DispatchQueue(label: “com.concurrent.ekram”, qos: DispatchQoS.background, attributes: [.initiallyInactive,.concurrent])
Now the result :
So Here newQueue is initially inactive and concurrent too when we execute it on viewDidApper with active() method its run concurrently .
Creating Global Queue :
We already know how to create custom queue with property . Actually don’t we don’t need to create global queue because GCD Engineer alredy created global queue for us.So We Can Create like This Way ..
let globalQueue = DispatchQueue.global()
And We can add to global Queue method
globalQueue.async { for i in 0..<10 { print(“Custom Green Love: 💚”,i) }
}
We can specify the global queue’s qos
let globalQueue = DispatchQueue.global(qos: .background)
If We Don’t specify qos it will be default qos
Thats all for this article even we can create queue which can make queue delay will start after initialised time .But we will explain at in next advance GCD article .But here we need to differentiate some topic which will help us :
Synchronous vs Asynchronous:
Synchronous :
- Synchronous method wait for the return still complete task Than return.
- Synchronous block current thread for the complete task
Asynchronous:
- asynchronous return immediately without waiting for the complete task .
- asynchronous don’t block current thread its return immediately