I have some task which I want to do asynchronously. So I am creating dispatchqueue in one class and doing some task.
//Adding Observer
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "NotificationIdentifier"), object: nil, queue: nil, using:catchNotification).
.
//Creating Background Thread.
let anotherQueue = DispatchQueue(label: "Thread1", qos: .background)
//Asynch thread.
anotherQueue.async {
for i in 0..<50 {
print("\n ",i)
sleep(1)
}
}
but upon some events I want to stop this, and am handling something like this.
func catchNotification (notification:Notification) -> Void {
//Stop the queue.
anotherQueue.suspend()
}
Am posting notification some other class. notification is getting received. but thread is not getting suspended.
Here is the complete class if you want to refer.
LoginPresenter.swift
let anotherQueue = DispatchQueue(label: "Thread1", qos: .background)
class LoginPresenter : LoginInteractor {
func catchNotification (notification:Notification) -> Void {
//Stop the queue.
anotherQueue.suspend()
}
func LoginSuccessFull()
{
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "NotificationIdentifier"), object: nil, queue: nil, using:catchNotification)
//Asynch thread.
anotherQueue.async {
//Synch
for i in 0..<50 {
print("\n ",i)
sleep(1)
}
}
}
or is any other better method or mechanism is there to do this ?