简体   繁体   English

在 swift 3 中使用 NotificationCenter 停止 Dispatchqueue

[英]Stop Dispatchqueue using NotificationCenter in swift 3

I have some task which I want to do asynchronously.我有一些我想异步完成的任务。 So I am creating dispatchqueue in one class and doing some task.所以我在一个班级中创建 dispatchqueue 并做一些任务。

//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登录Presenter.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 ?或者有没有其他更好的方法或机制可以做到这一点?

Reading Apple`s GCD documentation , you could see the following:阅读Apple 的 GCD 文档,您可以看到以下内容:

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:只需在LoginSuccessFull函数中创建第二个块,例如:

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

If you suspend straight after the function call, this block will not be executed.如果在函数调用后直接挂起,则不会执行该块。

If you would like to cancel immediately the operations in GCD , you will have to write you own implementation.如果您想立即cancel GCD的操作,则必须编写自己的实现。 But to how , it is always the one question for GCD , because it is just not designed that way.但是如何,它始终是GCD的一个问题,因为它不是那样设计的

The other option would be to use NSOperationQueue , but there you have implement right the cancel logic for your NSOperation subclasses.另一种选择是使用NSOperationQueue ,但您已经为您的NSOperation子类实现了cancel逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Swift 3 NotificationCenter 观察者中使用选择器 - Using selector in Swift 3 NotificationCenter observer 在Swift 4中异步使用dispatchQueue加速 - dispatchqueue using in asynchronously to speedup in Swift 4 如何在使用DispatchQueue时停止在swift上从主线程执行正在运行的后台线程 - How to stop execution of a running background thread from main thread on swift while using DispatchQueue 迅速停止DispatchQueue.global()。async任务 - Unable to stop DispatchQueue.global().async task on swift 如何使用swift 3.0中的NotificationCenter和swift 2.0中的NSNotificationCenter传递数据? - How to pass data using NotificationCenter in swift 3.0 and NSNotificationCenter in swift 2.0? 观察者不会使用NotificationCenter Swift 4.0打电话 - Observers don't get called using NotificationCenter Swift 4.0 使用 NotificationCenter 通知集合视图单元更改其子视图 - swift - Notify a Collection View Cell to changes its subviews using NotificationCenter - swift Swift - 使用 NotificationCenter 来观察值变化真的很糟糕吗? - Swift - Is using NotificationCenter to observe value change is really bad? Swift - 将 UserDefaults 添加到 NotificationCenter - Swift - Adding UserDefaults to NotificationCenter 通知中心观察者未在 swift 4 中调用 - NotificationCenter obersever not called in swift 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM