Ios 使用swift 3中的NotificationCenter停止Dispatchqueue

Ios 使用swift 3中的NotificationCenter停止Dispatchqueue,ios,swift3,dispatch-async,Ios,Swift3,Dispatch Async,我有一些任务要异步完成。所以我在一个类中创建dispatchqueue并执行一些任务 //Adding Observer NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "NotificationIdentifier"), object: nil, queue: nil, using:catchNotification). //正在创建后台线程。 让另一个队列=Dispat

我有一些任务要异步完成。所以我在一个类中创建dispatchqueue并执行一些任务

//Adding Observer      
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "NotificationIdentifier"), object: nil, queue: nil, using:catchNotification).

//正在创建后台线程。
让另一个队列=DispatchQueue(标签:“Thread1”,qos:.后台)
//异步线程。
anotherQueue.async{
对于0..读数中的i,您可以看到以下内容:


暂停发生在调用时运行的任何块完成后

这意味着,将执行已启动的每个块。只需在
loginsucessfull
函数中创建第二个块,如下所示:

如果在函数调用后立即挂起,则不会执行此块

如果您想立即
取消
GCD
中的操作,您必须编写自己的实现。但是,对于
GCD
,这始终是一个问题,因为不是这样


另一个选项是使用
NSOperationQueue
,但是您已经为
NSOperation
子类实现了
cancel
逻辑。

相反,您可以在登录屏幕上向用户显示ActivityView,然后在收到捕获通知后立即停止并关闭viewControllerspension发生在调用时运行的任何块完成后。这是写入的。
//Creating Background Thread.
     let anotherQueue = DispatchQueue(label: "Thread1", qos: .background)

    //Asynch thread.
    anotherQueue.async {
        for i in 0..<50 {
       print("\n Reading Apple`s GCD documentation, you could see the following:

The suspension occurs after completion of any blocks running at the time of the call.

This means that, every block will be executed, what has already been started. Just create a second block in your
LoginSuccessFull
function like:

anotherQueue.async {
  print("this is the second block")
}