私は非同期でやりたいことがいくつかあります。だから私は1つのクラスでディスパッチ・キューを作成し、いくつかのタスクを実行しています。すぐにNotificationCenterを使用してディスパッチキューを停止する3
//Adding Observer
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "NotificationIdentifier"), object: nil, queue: nil, using:catchNotification).
。
しかし、私はこれをやめて、このようなことをやりたいと思っています。
func catchNotification (notification:Notification) -> Void {
//Stop the queue.
anotherQueue.suspend()
}
投稿する他のクラスです。通知が受信されています。スレッドは中断されていません。
参照したい場合はここに完全なクラスです。
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)
}
}
}
またはその他のより良い方法やメカニズムがこれを行うにはありますか? Apple`s GCD documentationを読む
これとは別に、ログイン画面でActivityViewを使用しているユーザーを表示してから、キャッチ通知を受け取ると、それを停止してviewControllerを閉じることができます。 –
中断は、コール時に実行中のブロックの完了後に発生します。これはhttps://developer.apple.com/reference/dispatch/dispatchobject/1452801-suspendに書かれています。 –